Game Development Reference
In-Depth Information
Figure 8-2. The projectiles firing in a steady stream when Fire1 is held down
The next step in scripting the projectile functionality is to limit the rate at which the projectiles can be
fired. Many games will also limit the amount of ammunition the player will have, but with the enemies
multiplying quickly and a limited battery life, your Gnomatic Garden Defender will need all the help
he can get. And potatoes are rarely in short supply.
To gain control over the assault, you will be making a little timer. Unlike the coroutine used to
spawn the zombie bunnies, for the projectiles, you will be tapping into the system time, Time.time ,
to specify the exact time the next projectile will be available. To make the timer, you will create a
variable for the reload rate and for the target time.
1.
Create the following variables in the PotatoLauncher script:
float loadRate = 0.5f; // how often a new projectile can be fired
float timeRemaining; // how much time before the next shot can happen
Change the contents of the Update function as follows:
2.
timeRemaining -= Time.deltaTime; //
// if the Fire1 button (default is left ctrl) is pressed and the alloted time has passed
if (Input.GetButton ("Fire1") && timeRemaining <= 0) {
timeRemaining = loadRate; // reset the time remaining
ShootProjectile ();// ...shoot the projectile
}
Here is how it works: timeRemaining is the default 0 [seconds] the first time through, so the player can
shoot immediately. Time.deltaTime is approximately 1 second divided by the frame rateā€”so for 60
frames per second, or fps , it would be 1/60th of a second. If the player doesn't shoot, Time.deltaTime ,
 
Search WWH ::




Custom Search