Can someone please explain what this code does?

I’m making a shooting script and I’m following a tutorial but I don’t quite understand what this does

public float fireRate = .25f;
private float nextFire;

if (Input.GetButtonDown ("Fire1") && Time.time > nextFire) 
{ 
	nextFire = Time.time + fireRate;
}

I’ve never used Time.time so I don’t really know what it does

Thanks in advance :slight_smile:

Time.time as the documentation states is the time in seconds since the beginning of the game execution and after all Awake calls.

The script is tracking this + an offset for the rate of fire. If you didn’t do this it would then be possible to fire per frame in your game.
In this particular case it’s adding a quarter of a second and then testing, this is done on the first and subsequent fire after.

So if your first firing was @ 2.50 seconds into the game, it would be stored as 2.50+0.25(which equals 2.75) in the variable nextFire.

Each frame the value of the nextFire is evaluated to be less then Time.time(totally amount of time since game start). That means the delay of a quarter
second has passed and the player is able to fire again. The next time though it will be whatever time has elapsed + a quarter of a second again.