Wednesday, June 29, 2011

Unity FPS Shooter Game tutorial #2

Making the missile explode
======================================================================



var explosion : GameObject;
function OnCollisionEnter( collision : Collision )
{
 var contact : ContactPoint = collision.contacts[0];
 var rotation = Quaternion.FromToRotation( Vector3.up, contact.normal );
 var instantiatedExplosion : GameObject = Instantiate(
  explosion, contact.point, rotation );
 Destroy( gameObject );
}

========================================================================================================================================================

Finally,
we
 want 
to 
make
 the 
missile 
disappear 
from 
the
 game 
as 
it
 has 
now 
collided,

which 
is
 done 
by 
calling 
the 
Destroy()
 function 
with 
gameObject 
as 
a 
parameter

(gameObject 
denotes 
the
 object 
the 
script 
is 
associated
 with)


=====================================================================================================================================================


Add 
the
 code 
into 
the 
Projectile
 Javascript
 and
 save 
it.
Attach
 the
 Projectile
 Javascript 
to 
the
 Missile
 prefab 
by 
selecting
 Component
­>

Scripts
­>
Projectile.
Now 
we 
have 
to
 create 
the 
explosion 
that
 we
 want 
to 
occur
 when ever
 the
 missile 
col­
lides.
First,
create 
a
 new 
Prefab
 (call 
it 
Explosion)
 that 
will 
store 
the 
explosion 
asset.
The
 standard
 assets 
contain 
a 
nice 
explosion 
prefab 
with 
a 
particle
 system 
and 
a

light 
around 
the 
explosion.
Drag 
the 
explosion 
prefab 
under 
Standard
Assets/
Particles/explosion
 into 
the 
Hierarchy
 View.
Adjust
 the 
settings 
on 
the 
explosion 
until 
you’re 
happy 
with 
how 
it 
looks,
then

drag
 the 
Explosion 
from
 the 
Hierarchy 
view 
on to 
the 
Explosion 
Prefab
 in 
the

Project
 panel.
Now
 we 
can 
assign 
the 
explosion 
to 
the 
missile:
Ensuring
 that 
Missile 
prefab 
is 
selected,
 fill
 in 
the 
Explosion 
variable 
by 
dragging

the 
Explosion 
object 
in
 the 
Project 
panel 
onto
 the 
Missile’s
 Explosion 
variable
 in

the
 Inspector 
panel


No comments:

Post a Comment