1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465 |
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
- public class ShootLogic : MonoBehaviour
- {
- [Tooltip("子弹出口")]
- public GameObject shootPoint;
- [Tooltip("弹舱")]
- public Transform magazine;
- [Tooltip("子弹预设体")]
- public GameObject bulletPrefab;
- [Tooltip("炮筒")]
- public GameObject barrel;
- [Tooltip("炮筒旋转速度")]
- public float turnSpeed = 5;
-
- // Start is called before the first frame update
- void Start()
- {
-
- }
- // Update is called once per frame
- void Update()
- {
- if (Input.GetKeyDown(KeyCode.Space)) this.InvokeRepeating("Shoot", 0.5f, 0.5f);
- if (Input.GetKeyUp(KeyCode.Space)) this.CancelInvoke("Shoot");
- if (Input.GetKeyDown(KeyCode.A)) this.InvokeRepeating("LeftTurn", 0, 0.1f);
- if (Input.GetKeyUp(KeyCode.A)) this.CancelInvoke("LeftTurn");
- if (Input.GetKeyDown(KeyCode.D)) this.InvokeRepeating("RightTurn", 0, 0.1f);
- if (Input.GetKeyUp(KeyCode.D)) this.CancelInvoke("RightTurn");
- if (Input.GetKeyDown(KeyCode.W)) this.InvokeRepeating("UpTurn", 0, 0.1f);
- if (Input.GetKeyUp(KeyCode.W)) this.CancelInvoke("UpTurn");
- if (Input.GetKeyDown(KeyCode.S)) this.InvokeRepeating("DownTurn", 0, 0.1f);
- if (Input.GetKeyUp(KeyCode.S)) this.CancelInvoke("DownTurn");
- }
- void Shoot()
- {
- GameObject bullet = Object.Instantiate(bulletPrefab, magazine);
- bullet.transform.position = shootPoint.transform.position;
- bullet.transform.eulerAngles = barrel.transform.eulerAngles;
- }
- void LeftTurn()
- {
- if (this.transform.eulerAngles.y > 300 || this.transform.eulerAngles.y < 60) this.transform.Rotate(0, -turnSpeed, 0, Space.Self);
- }
- void RightTurn()
- {
- if (this.transform.eulerAngles.y > 300 || this.transform.eulerAngles.y < 60) this.transform.Rotate(0, turnSpeed, 0, Space.Self);
- }
- void UpTurn()
- {
- if (this.transform.eulerAngles.x > 330) this.transform.Rotate(-turnSpeed, 0, 0, Space.Self);
- }
- void DownTurn()
- {
- if (this.transform.eulerAngles.x < 30) this.transform.Rotate(turnSpeed, 0, 0, Space.Self);
- }
- }
|