Game Development Reference
In-Depth Information
Step 10. Save and play. As the rocket moves in for the attack it starts
creating bullets from the prefab. The bullet prefab has the moveBullet
script attached to it that moves the bullet in a forward motion. However,
because the bullet is created at the rocket's location and orientation, it
travels forward relative to the rocket and therefore in the direction the
rocket is facing.
Step 11. The current attack script will continue creating bullets as long
as the program runs. Each bullet is a new game object. As the game runs
you will see the Hierarchy fill up with bullet clones. This can become
problematic as the game gets clogged up with game objects, many of
which are off the screen and traveling toward infinity. They need to be
destroyed. To do this, add the code shown in Listing 2.8 to the attack
Listing 2.8 Destroying a Game Object after 0.5 Seconds
function Update ()
. . .
if(state == "ATTACK")
var newBullet:GameObject = Instantiate(aBullet,
. . .
Step 12. Save and play. The Destroy() function will delete the specified
game object after the given amount of seconds (in this case 0.5 seconds).
As the Destroy() function needs to know which object to destroy, a local
variable is created to hold the instantiated game object. This is then
passed to Destroy().
Step 13. Instead of a continued stream of bullets it will look better if there
is a delay between them. You can adjust the delay to whatever reloading
speed you want. To add this functionality, modify the attack script to that
in Listing 2.9 .
Search Nedrilad ::

Custom Search