Game Development Reference
In-Depth Information
Step 14. To add some impact explosions, import the
detonator package ( Detonator.unitypackage ) used in
Chapter Two.
Step 15. Create a JavaScript file called explode and enter the
code shown in Listing 4.17 . This is the same code used for the
flattening sphere bombs in Chapter Two but with the flattening
part removed.
Listing 4.17 Creating an Explosion on Impact
var explosion:GameObject;
var explosionLife:float = 5.0;
var detailLevel: float = 1.0;
var countdown: float = 5.0;
private var explosionDirection: Quaternion;
private var explosionLocation: Vector3;
function OnCollisionEnter(collision : Collision)
{
rigidbody.isKinematic = true;
Destroy (this.collider);
var contact = collision.contacts[0];
var rot = Quaternion.FromToRotation(Vector3.up, contact.
normal);
explosionDirection = rot;
var offsetSize = explosion.GetComponent("Detonator").
size / 3;
explosionLocation = contact.point + ((Vector3.Scale(contact.
normal,
Vector3(offsetSize,offsetSize,offsetSize))));
timeSet = Time.fixedTime + countdown;
explosionActivated = true;
if(collision.gameObject.name ! = "Terrain")
{
Destroy(collision.gameObject,1);
}
Explode();
Destroy(this.gameObject);
}
function Explode()
{
var exp : GameObject = Instantiate (explosion,
explosionLocation,
explosionDirection);
exp.GetComponent("Detonator").detail = detailLevel;
Destroy(exp, explosionLife);
}
Search Nedrilad ::




Custom Search