This is commonly called a “cooldown timer.” You can google for examples and tutorials, but the simplest way is this:
-
have a
private float gunHeat;
variable -
In Update(), if that variable is greater than zero, subtract Time.deltaTime from it (this cools down the
gunHeat
)
if (gunHeat > 0)
{
gunHeat -= Time.deltaTime;
}
- When you go to fire:
— A. only fire if thatgunHeat
variable is zero or less.
— B. set that variable to the time (in seconds) you want to have between shots.
if (UserWantsToFireTheGun)
{
if (gunHeat <= 0)
{
gunHeat = 0.25f; // this is the interval between firing.
ActuallyDoTheFiring();
}
}