Game Development Reference
In-Depth Information
Step 17. To cause a small explosion when a bullet hits the base,
modify moveBullet to instantiate an explosion game object when it
hits, as shown in Listing 2.11 .
Listing 2.11 Instantiating an Explosion
var speed = 2.0;
private var target:GameObject;
var explosion: GameObject;
function Start ()
{
target = GameObject.Find("Base");
}
function Update ()
{
this.transform.Translate(Vector3.forward * speed);
if(Vector3.Distance(target.transform.position,
this.transform.position) < 5)
{
var exp:GameObject = Instantiate(explosion,
this.transform.position,
this.transform.rotation);
Destroy(exp,1.0);
Destroy(this);
}
}
Step 18. Save. Before playing, select the moveBullet script from the
Project and then locate the explosion Prefab and drag it onto the
exposed variable Explosion in the Inspector window.
Step 19 . Play. When a bullet hits the base, an explosion will occur
instead of print. The explosion prefab is a particle system. You will
learn more about these in later chapters. Just after the explosion
is instantiated and set to be destroyed, the script also destroys the
bullet game object. This will ensure that the bullet doesn't stay alive in
the game environment after it has hit the base.
In this hands-on session you've learned some basic uses of vectors in
a simple 2D game environment.
Search Nedrilad ::




Custom Search