| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061 |
- using UnityEngine;
- using UnityEngine.InputSystem;
- public class GunController : MonoBehaviour
- {
- [SerializeField]
- private Transform muzzle;
- [SerializeField]
- private InputActionProperty trigger;
- public float shotsPerSecond = 1.0f;
- public float projectileSpeed = 1.0f;
- public float projectileLifeTime = 1.0f;
- public GameObject projectileObject;
- private float lastShotTimestamp;
- private float lastTriggerPress;
- // Start is called once before the first execution of Update after the MonoBehaviour is created
- void Start()
- {
- lastShotTimestamp = -1 * (1 / shotsPerSecond);
- }
- // Update is called once per frame
- void Update()
- {
- if (trigger.action.WasPressedThisFrame() && Time.time - lastShotTimestamp >= 1/shotsPerSecond)
- {
- GameObject spawnedObject = Instantiate(projectileObject);
- Projectile projectileScript = spawnedObject.GetComponent<Projectile>();
- GameObject dynamicContainer = GameObject.Find("_Dynamic");
- if (dynamicContainer != null)
- {
- spawnedObject.transform.parent = dynamicContainer.transform;
- }
- spawnedObject.transform.position = muzzle.position;
- spawnedObject.transform.rotation = transform.rotation;
- if (projectileScript != null) {
- projectileScript.maxLifeTime = projectileLifeTime;
- projectileScript.speed = projectileSpeed;
- }
- lastShotTimestamp = Time.time;
- Debug.Log("Shoot");
- }
- }
- }
|