Because the cannonball is not exploding, you can set a timer in the Start function to destroy it after a set amount
of time.
1. Create a new script in the Adventure Scripts folder.
2. Name it Terminator.
3. Add a variable for life:
var life : float = 5.0; // time before destroying the object
4. In the Start function, add the following:
// wait for life seconds before destroying the object the script is on
yield new WaitForSeconds(life);
Destroy(this.gameObject);
5. Save the script.
6. Drag the script onto the Cannonball prefab in the Project view.
Because the timer only starts when the object is instantiated, you can use simple pause created by the yield’s
WaitForSeconds function.
7. Click Play and shoot off a few rounds.
Now the cannonballs wink out of existence in both the scene and the Hierarchy view after the required 5 seconds.
Many projectiles, when accompanied by an explosion, are destroyed on their first collision, but that takes a bit more code.
With some very basic projectile functionality covered, let’s turn off the Proxy Weapon for now and create
something that you will be using later in the game.
8. Select the Proxy Weapon and Deactivate it.
RockFall
You’ve already created a simple prefab, the cannonball. Let’s take a minute to look up Instantiate in the Scripting
reference. For the cannonball, you used the first variation. It is instantiated at the transform of the object the script
calling it is on.
If you wanted to do something like instantiate a Genie in a puff of smoke in a fixed position, you’d leave off the
position and rotation arguments, and the instance would be created using the prefab’s original transforms—from
when it was set up in the scene. In this case, the last form of instantiate would be the one you will want to use. Let’s try
that with something more down to earth, like a rock.
Because the code is already written for you in the scripting reference, you may as well trigger the instantiation
with OnTriggerEnter.
1. Create a Cube near the test objects.
2. Check Is Trigger on its Collider component.
3. Name it Rock Zone.
4. Scale the cube to about 2×2×3.
5. Disable the Mesh Renderer.
6. Create a new script in the Adventure Scripts folder.
through the thin walls of the chest base’s Mesh Collider between one frame and the next when collision is tested.
The default Collision Detection for Rigidbodies is Discreet. To get it to check more often for just the projectile, you can
use Continuous.
2. Select the Cannonball in the Project view.
3. Change its Collision Detection to Continuous.
4. Click Play and shoot the chest.
This time the cannonballs all hit and bounce.
As you probably guessed, Continuous uses a lot more resources than the default Discreet, but because the
weapon is only fired intermittently, it shouldn’t pose a problem. For more on physics and how it is calculated in game,
be sure to check the documentation.
Finally, let’s take a look at Instantiate’s counterpart, Destroy. If you are to trigger an explosion when and where
the cannonball hit something (more like a grenade behavior), you would want the projectile to go out of the scene
shortly after the hit. To kill off an object permanently, you would use Destroy on collision
409