ShootLogic.cs 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. public class ShootLogic : MonoBehaviour
  5. {
  6. [Tooltip("子弹出口")]
  7. public GameObject shootPoint;
  8. [Tooltip("弹舱")]
  9. public Transform magazine;
  10. [Tooltip("子弹预设体")]
  11. public GameObject bulletPrefab;
  12. [Tooltip("炮筒")]
  13. public GameObject barrel;
  14. [Tooltip("炮筒旋转速度")]
  15. public float turnSpeed = 5;
  16. // Start is called before the first frame update
  17. void Start()
  18. {
  19. }
  20. // Update is called once per frame
  21. void Update()
  22. {
  23. if (Input.GetKeyDown(KeyCode.Space)) this.InvokeRepeating("Shoot", 0.5f, 0.5f);
  24. if (Input.GetKeyUp(KeyCode.Space)) this.CancelInvoke("Shoot");
  25. if (Input.GetKeyDown(KeyCode.A)) this.InvokeRepeating("LeftTurn", 0, 0.1f);
  26. if (Input.GetKeyUp(KeyCode.A)) this.CancelInvoke("LeftTurn");
  27. if (Input.GetKeyDown(KeyCode.D)) this.InvokeRepeating("RightTurn", 0, 0.1f);
  28. if (Input.GetKeyUp(KeyCode.D)) this.CancelInvoke("RightTurn");
  29. if (Input.GetKeyDown(KeyCode.W)) this.InvokeRepeating("UpTurn", 0, 0.1f);
  30. if (Input.GetKeyUp(KeyCode.W)) this.CancelInvoke("UpTurn");
  31. if (Input.GetKeyDown(KeyCode.S)) this.InvokeRepeating("DownTurn", 0, 0.1f);
  32. if (Input.GetKeyUp(KeyCode.S)) this.CancelInvoke("DownTurn");
  33. }
  34. void Shoot()
  35. {
  36. GameObject bullet = Object.Instantiate(bulletPrefab, magazine);
  37. bullet.transform.position = shootPoint.transform.position;
  38. bullet.transform.eulerAngles = barrel.transform.eulerAngles;
  39. }
  40. void LeftTurn()
  41. {
  42. if (this.transform.eulerAngles.y > 300 || this.transform.eulerAngles.y < 60) this.transform.Rotate(0, -turnSpeed, 0, Space.Self);
  43. }
  44. void RightTurn()
  45. {
  46. if (this.transform.eulerAngles.y > 300 || this.transform.eulerAngles.y < 60) this.transform.Rotate(0, turnSpeed, 0, Space.Self);
  47. }
  48. void UpTurn()
  49. {
  50. if (this.transform.eulerAngles.x > 330) this.transform.Rotate(-turnSpeed, 0, 0, Space.Self);
  51. }
  52. void DownTurn()
  53. {
  54. if (this.transform.eulerAngles.x < 30) this.transform.Rotate(turnSpeed, 0, 0, Space.Self);
  55. }
  56. }