Game Development Reference
In-Depth Information
9. Turn on the GardenGates collider component's Is Trigger parameter.
Next you will create a small script to trigger the gates' animation so the doors will open and close
when the garden defender enters and exits the collider. It is very similar to the last script you created in
Chapter 5. Once it confirms that the object triggering it is tagged as Player, it will trigger the animation.
1.
Create a new folder in the Assets folder, and name it Game Scripts .
2.
Create a new C# script in it, and name it SensorDoors .
3.
Open it in the script editor, and add the following two variables below the
class declaration:
public AnimationClip clipOpen; // the open animation
public AnimationClip clipClose; // the close animation
Below the Update function, add an OnTriggerEnter() function:
4.
// open the gates
void OnTriggerEnter (Collider defender) {
if (defender.gameObject.tag == "Player") {
animation.Play(clipOpen.name);
}
}
Duplicate the OnTriggerEnter code, and adjust it for an OnTriggerExit :
5.
// close the gates
void OnTriggerExit (Collider defender) {
if (defender.gameObject.tag == "Player") {
animation.Play(clipClose.name);
}
}
This script will go on the GardenGates object that also contains the Animation component, so you
can trigger the animation clip you want with only the component name and the Play() function. The
clip is actually accessed by name, so you must use .name . Feel free to look up “animation.Play” in
the Scripting Reference.
1.
Save the script, and drag it onto the GardenGates object.
2.
Change the bench's tag to Player from the drop-down Tag list.
3.
To prevent the bench from stopping at the gateway, delete the Cube.
You might think that disabling the ColliderTests script will keep it from doing anything, but the only
things it turns off for certain are Start and Awake functions.
4.
Remove the ColliderTests script from the StoneGardenBench object.
5.
Select the GardenGates object, and load the two animation clips into the Clip
Open and Clip Close parameters using the browse icon at the far right of each.
Search WWH ::




Custom Search