A simple solution is what I call a timestamp. Note there is a tiny bit of inaccuracy in the calculation but used for this, it won’t be noticed by the user:
using UnityEngine;
using System.Collections;
public class playerShoot : MonoBehaviour
{
public GameObject bullet;
public float timeBetweenShots = 0.3333f; // Allow 3 shots per second
private float timestamp;
void Update ()
{
if (Time.time >= timestamp && (Input.GetKeyDown(KeyCode.Space)) )
{
Instantiate(bullet, transform.position, transform.rotation);
timestamp = Time.time + timeBetweenShots;
}
}
}
ohh thanks! it seemed to have worked!
– HercULASDo not forget to accept answers if they are correct. Also, sometimes, if you need to have more correct time limit, you will need to move from Update to FixedUpdate and change Time.time with Time.fixedTime.
– AurimasBlazulionis