Game Development Reference
In-Depth Information
From the preceding code, we get the array of the children of rigidbody by using the
gameObject.GetComponentsInChildren.<Rigidbody>() funcion. Next, we will add
the code to make the rocks stop moving when they fall down and their velocity is close to
zero by adding the following code:
// Update every frame
public function Update () : void {
if (b_isTrigger == true) {
for (var r : Rigidbody in a_rigid) {
if (r.isKinematic == false) {
var f_sqrLen : float = (r.velocity).sqrMagnitude;
if (f_sqrLen <= 0.0) {
r.useGravity = false;
r.isKinematic = true;
in_count++;
}
}
}
//Stop updating if all the rocks stop moving
if (in_count >= a_rigid.Length) {
b_isTrigger = false;
}
}
}
As the last step of the script, we will add the funcion for geing and seing b_isTrigger ,
and also enable and disable rigidbody , as shown in the following script:
public function GetTrigger() : boolean {
return b_isTrigger;
}
public function SetTrigger( _isTrigger : boolean) : boolean {
b_isTrigger = _isTrigger;
}
public function EnabledRigidbody () : void {
for (var r : Rigidbody in a_rigid) {
r.useGravity = true;
r.isKinematic = false;
//Apply the velocity to the rigidbody in the -y direction to make
the object fall faster
r.velocity = new Vector3(0, -downForce, 0);
}
}
 
Search WWH ::




Custom Search