Game Development Reference
In-Depth Information
public function DisabledRigidBody() : void {
for (var r : Rigidbody in a_rigid) {
r.useGravity = false;
r.isKinematic = true;
}
}
We will use this
Rocks
script with our
Break
game object in the
Rockslide
to enable and
disable
rigidbody
of its children by seing the
isKinematic
to
false
or
true
.
Next, we will need another script to control the trigger area, which will trigger the
Rockslide
falling down. Go back to Unity and go to
Assets
|
Create
|
Javascript
to create a
new script and name this new script
TriggerArea
, and replace the code as follows:
public var rocks : Rocks;
public function OnTriggerEnter(collider : Collider) : void {
if ((collider.transform.tag == "Player") && (rocks.GetTrigger() ==
false)) {
rocks.EnabledRigidbody();
rocks.SetTrigger(true);
}
}
The preceding code basically tells us that if the player enters the trigger area, we will enable
the
rigidbody
and make the rock fall down.
Go back to Unity and we will do the last step, which is ataching the script to the
Rockslide
game object. Let's click on the
Break
game object inside
Rockslide
and drag the
Rocks
script on it.
Then, we will click on the
TriggerArea
object inside
Rockslide
and drag the
TriggerArea
script that we just created on it. Then, we will go to the
TriggerArea
Inspector
view and set the following:
F
Trigger Area (Script)
:
Rocks
:
Break
(Drag
Break
game object, the child of the
Rockslide
object,
to the
Hierarchy
here)