Game Development Reference
In-Depth Information
F
Transform (Cube4)
Position
:
X: -3
Y: 23.5
Z: 0
6. We will click on the
Wall
game object in the
Hierarchy
view and go to the
Inspector
view and set up its
Transform
as follows:
F
Transform
Position
:
X: 1037.5
Y: -16.5
Z: 693
Rotation
:
X: 0
Y: 36
Z: 0
Scale
:
X: 1
Y: 1
Z: 1
7. Now, we will create the script that makes this wall object break apart when the
character shoots at it. Let's go to the
Chapter7/Scripts
folder and double-click
the
rocket
script to open this script in the script editor.
8. Inside the
rocket
script, we will add two parameters at the beginning of the script,
as highlighted in the following code:
@script RequireComponent(ConstantForce)
//Add the explosion force and radius
public var explosionRadius : float = 50;
public var explosionForce : float = 1000;
9. Next, we will go to the
OnCollisionEnter (others : Collision)
funcion
and add the following highlighted script:
public function OnCollisionEnter (others : Collision) : void {
//Create the explosion on the first impact point of the rocket
and collider
var contactPoint : ContactPoint = others.contacts[0];
var rotation : Quaternion = Quaternion.FromToRotation(Vector3.
up, contactPoint.normal);
GameObject.Instantiate(explosionParticle, contactPoint.point,
rotation);
//Get the transform position of the rocket
var v3_position : Vector3 = transform.position;
//Get all colliders that touches or insides the explosion radius
var a_hits : Collider[] = Physics.OverlapSphere(v3_position,
explosionRadius);
for (var c : Collider in a_hits) {
// Check tag
if (c.tag == "Destructible") {
//Get all rigidbody of the colliders
var r : Rigidbody = c.rigidbody;
if (r != null) {
//Explosion
r.isKinematic = false;
r.AddExplosionForce(explosionForce, v3_position,
explosionRadius);