Game Development Reference
In-Depth Information
In the Start function, call a function to set the bundle to its starting state:
4.
Initialize(); // set location
Create the Initialize function:
5.
void Initialize () {
// set the animation state back to Bundle Carry clip/state
animator.Play ("Bundle Carry");
}
Using animator.Play , you set the Bundle Carry state/clip to the currently active state in the
Animator. Note that animator.Play , unlike audio.Play , requires the name of the clip, so the name is
in quotation marks. As you have seen before, this also provides you with a means to put an object
directly into a new state without it going through a transition that is meaningless for sprites.
To detect the collision, you will use the 2D version of OnCollisionEnter . From that function, you
will tell the bundle to go directly into the Open clip/state. Then you will call a coroutine to wait
for 2 seconds and re-parent the object to the Stork Group. The trick here is that by re-parenting
the bundle after the stork has left the scene and been deactivated, the bundle will inherit that
deactivation without its own being changed. When the Stork Group is activated for the next pass,
the Bundle will automatically be activated again because its own Active parameter was never
turned off.
6.
Add the collision detector:
void OnCollisionEnter2D () {
animator.Play ("Bundle Open"); // trigger the open clip
StartCoroutine(Deactivator()); // start the coroutine
}
And add the Deactivator function:
7.
IEnumerator Deactivator () {
yield return new WaitForSeconds(3.5f); // wait 3.5 seconds
// turn off the physics
rigidbody2D.isKinematic = true;
// add the bundle back into the Stork group' transform
transform.parent = stork.transform;
// reset the start position
Vector3 tempLocation = transform.position;
tempLocation = startLocation;
transform.localPosition = tempLocation;
}
8.
Save the script, and add it to the Bundle object.
9.
In the Inspector, drag the Stork Group in as the Stork and the Bundle in for
the [Bundle's] Animator.
 
Search WWH ::




Custom Search