Laser Cool Down
This is going to limit the rate of laserfire for the player.
Declare two serialized private floats:
And we’re using Time.time to limit it — that is, “the time in seconds since the start of the application” (Unity Documentation Time.time).
Below is how to include it in the code in the player script:
Time.time — the number of seconds the game is active — must exceed -1f — which is always true because time does not travel backward.
So now the code that runs when this is true has -1f equaling the number of seconds of activity added to .5f which allows the player to fire one shot every half a second.
I honestly don’t understand the math on this one, I just know it works.
CODE CLEAN-UP
Now let’s optimise our code a bit.
Under the CaculateMovement function in the player script, let’s make one just for laserfire:
And then its own void — void fireLaser():
Join us next time as we start working on THE ENEMY.