Game Development Reference
In-Depth Information
Advanced Weaponry
As the saying goes, “ Close only counts in horseshoes, hand grenades and nuclear weapons.” As
it stands, your player must make a direct hit to wipe out a zombie bunny. Given their tendency to
cluster, it would help your player if the blast would work off of proximity rather than direct hits. In this
section, you will alter the projectile code to do just that.
1.
Open the Projectile script.
2. Comment out the first two lines in the OnCollisionEnter function.
In the earlier version, you simply used the hit point as the location to instantiate the explosion. In
this version, you will also get and use the orientation of the object at the hit or contact point. It's
not necessary for the giant fireball explosion, but if you wanted something more like sparks or
shrapnel, they would have to be oriented at the normal to the surface that was just hit. A normal is a
perpendicular to the face in the up or out direction. The normal to a piece of flat ground points upward.
The normal to the garden wall will be perpendicular to the direction that the wall's surface is facing.
Change the OnCollisionEnter line to include an argument:
3.
void OnCollisionEnter (Collision collision) {
4.
Below the commented lines, add the following:
//get the contact point
ContactPoint contact = collision.contacts[0];
//get the normal of the contact point
Quaternion rotation = Quaternion.FromToRotation(Vector3.up, contact.normal);
// instantiate an explosion at that point, using its normal as the orientation
Instantiate (explosion, contact.point,rotation);
This time you will be using the argument passed in to the OnCollisionEnter function to obtain
information about the collision.
5.
In the Scripting Reference, do a search for the Collision type.
The Collision type's parameters include various useful parameters (Figure 8-40 ). When a collision
event occurs, the following information is available:
Figure 8-40. The parameters for the Collision type
 
Search WWH ::




Custom Search