Game Development Reference
In-Depth Information
if (childColliders != null)
{
for (var childCollider : Collider in childColliders)
{
childCollider.enabled = true;
}
}
gameObject.collider.enabled = false;
gameObject.rigidbody.isKinematic = true;
gameObject.GetComponent(Animator).enabled = false;
gameObject.GetComponent(ThirdPersonCharacter).enabled = false;
gameObject.GetComponent(ThirdPersonUserControl).enabled = false;
}
This script breaks down as follows:
(1) private var childRigidBodies : Rigidbody[];
(2) private var childColliders : Collider[];
Here you are introduced to another programming concept: an
array
. An array is a set of the
same type of items, called
elements
. In (1), a private reference variable for an array named
childRigidBodies
is declared that will hold elements of type
Rigidbody
. The square brackets
[]
are
the syntax that makes this an array as opposed to a singular
Rigidbody
variable declaration. In this
example, the
childRigidBodies
array will hold as elements the child rigidbodies of the ragdoll.
Similarly in (2) a private reference variable for an array named
childColliders
is declared that will
hold elements of type
Collider
, in this case the child colliders of the ragdoll.
In the
Start()
function:
(3) childRigidBodies = gameObject.GetComponentsInChildren.<Rigidbody>();
GetComponentsInChildren
is aptly named as it returns all components of the designated type of both
the game object and any of its children. Here the rigidbody components of the character and child
rigidbodies created by the Ragdoll Wizard are assigned as elements of the
childRigidBodies
array.
(4) childColliders = gameObject.GetComponentsInChildren.<Collider>();
Likewise, the Third Party Character Ragdoll game object's Collider component and the child collider
components created by the Ragdoll Wizard are assigned as elements of the
childColliders
array.
In the
GotoRagdoll()
function:
(5) if (childRigidBodies != null)