Game Development Reference
In-Depth Information
the duration of a frame, is subtracted each frame, so it will remain less than 0. As long as timeRemaining
is less than 0, the player may shoot when ready. When the player does shoot, the timeRemaining is set to
the loadRate and he must wait for it to drop back down before he can shoot again.
Time.time is started when the game starts, so at any time during the game, you can use it to find
how much time has elapsed since the start of the game. In this code, to “set” the timer, you are
getting the current time since the start of the game and adding the rate or amount of time between
firing. It's like saying it's 3:05 now and in 15 minutes I want something to be able to happen. 3:05 + 15
makes the target time 3:20. When the player presses the Fire1 key or button, a new target time is set
and until that time has been reached, the condition to shoot another projectile is not met.
3.
Save the script, and test the new limitation by continuously holding down the
Fire1 button.
This time, the number of projectiles littering the scene is greatly reduced. The overload rate for filling
the scene with projectiles is lower, but the possibility of that happening remains. With the projectiles
under control, you might wish to add a nice little sound effect to go along with the gun firing.
4.
Add an Audio Source component to the Fire Point object, and uncheck “Play
on Awake.”
5.
Load GunPop as its Audio Clip.
6.
Back in the PotatoLauncher script, add the following after the line:
audio.Play (); // play the default audio clip on this component's gameObject
7.
Save the script.
8.
Click Play, and test the potato gun.
Unlike conventional ammunition that is designed to explode on contact, at most, potatoes might
break apart on contact. Fortunately, your varmint invasion consists of zombie bunnies, so the rules
of nature no longer apply!
You will begin by destroying the projectile at a set time after it is instantiated. With a normal
projectile, such as a bullet, it would be destroyed on first contact through an OnCollisionEnter
function. Potatoes, lacking an explosive charge, aren't so easy to get rid of. Typically, you will set
the projectile to be destroyed in the Start function in case it never hits anything, and also in an
OnCollisionEnter function in case of a valid hit. Later you will spice up the collision event with some
special effects. The good news is that the Destroy() function has a built-in timer, so you won't have
to fuss with coroutines to activate it after a given amount of time.
9.
Create a new C# Script in the Game Scripts folder, and name it Projectile .
Add the following to its Start function:
10.
//destroy the object this script is on 3 seconds after instantiation
Destroy(gameObject, 3f);
Remembering that the Start function is called when the gameObject is activated in the scene, not
when the game itself is started, you can see that the destroy method's timer starts as soon as the
projectile is instantiated.
 
Search WWH ::




Custom Search