Adding a Trigger to a Unity Animation...

19
1 Adding a Trigger to a Unity Animation – Method #2 Unity Version: 5.0 Adding the GameObjects In this example we will create two animation states for a single object in Unity with the Animation panel. Our goal is to animate the opening and closing of a door GameObject, then with C# (pronounced C Sharp) make the door open and close once our first person controller passes through a trigger collider. The collider remains stationary as the door opens and closes. For this tutorial we’ll add a sliding door in the middle of the wall Start by creating a new project in Unity. Save your scene in the Assets folder and name it DoorTrigger.

Transcript of Adding a Trigger to a Unity Animation...

Page 1: Adding a Trigger to a Unity Animation M2pixelstreetstudios.com/.../06/Adding-a-Trigger-to-a-Unity-Animation-M… · Unity Version: 5.0 Adding the GameObjects In this example we will

  1  

Adding a Trigger to a Unity Animation – Method #2 Unity Version: 5.0 Adding the GameObjects In this example we will create two animation states for a single object in Unity with the Animation panel. Our goal is to animate the opening and closing of a door GameObject, then with C# (pronounced C Sharp) make the door open and close once our first person controller passes through a trigger collider. The collider remains stationary as the door opens and closes.

For this tutorial we’ll add a sliding door in the middle of the wall Start by creating a new project in Unity. Save your scene in the Assets folder and name it DoorTrigger.

Page 2: Adding a Trigger to a Unity Animation M2pixelstreetstudios.com/.../06/Adding-a-Trigger-to-a-Unity-Animation-M… · Unity Version: 5.0 Adding the GameObjects In this example we will

  2  

Save and name your scene. Save it in the Project’s Assets folder. Add a plane GameObject (GameObject > 3D Object > Plane), then add a cube game object (GameObject > 3D Object > Cube). Scale the cube so it looks like a wall and position it so it rests on the plane. Duplicate the wall section by right-clicking the wall in the Hierarchy panel and choosing Duplicate. Move the duplicate so you have a gap between the sections to insert your door. Duplicate one of the wall sections and scale it down slightly to create a door, then drag the door to the gap.

Add two walls a door and a plane object to the scene.

Page 3: Adding a Trigger to a Unity Animation M2pixelstreetstudios.com/.../06/Adding-a-Trigger-to-a-Unity-Animation-M… · Unity Version: 5.0 Adding the GameObjects In this example we will

  3  

In the Hierarchy panel, name the game objects Ground, Wall01, Wall02 and Door.

Name the objects Ground, Wall01, Wall02 and Door The next step is to add a collider object. We’ll use an empty GameObject to hold the collider. Add an empty GameObject by choosing GameObject > Create Empty. Name it DoorCollider in the Hierarchy panel. Select the DoorCollider object in the Hierarchy panel and open the Inspector.

Select the DoorCollider object and open the Inspector Click the Add Component button at the bottom of the panel and choose Physics > Sphere Collider.

Page 4: Adding a Trigger to a Unity Animation M2pixelstreetstudios.com/.../06/Adding-a-Trigger-to-a-Unity-Animation-M… · Unity Version: 5.0 Adding the GameObjects In this example we will

  4  

Select Sphere Collider from the Physics Components Increase the Sphere Collider Radius to include the door and part of the wall.

Increase the Sphere radius in the Inspector panel, Sphere Collider section. Select the Is Trigger checkbox in the Sphere Collider component.

Select the Is Trigger checkbox

Page 5: Adding a Trigger to a Unity Animation M2pixelstreetstudios.com/.../06/Adding-a-Trigger-to-a-Unity-Animation-M… · Unity Version: 5.0 Adding the GameObjects In this example we will

  5  

Now the sphere can act as an invisible trigger to detect the presence of the first person controller. We’ll monitor whenever the first person controller either enters or exits the sphere to make the door open and close. Creating the Door Open and Close States in Unity Select the Door object in the Hierarchy panel and open the Animation panel (Window > Animation).

An empty Animation panel appears Click the Add Property button and name your animation clip DoorOpen.

Name your animation clip, DoorOpen Be sure to save it in the Assets folder. In the next panel, open the transform section and click the plus icon next to Position.

Page 6: Adding a Trigger to a Unity Animation M2pixelstreetstudios.com/.../06/Adding-a-Trigger-to-a-Unity-Animation-M… · Unity Version: 5.0 Adding the GameObjects In this example we will

  6  

Click the plus icon to the right of the word Position Notice that the game play controls display in red. Unity is now recording your changes to the animation clip.

The game play controls turn red With the timeline set to zero, move the door to the open position. Scrub the timeline to one second and use the move tool once again to move the door to the open position. For accurate placement you can also enter Position values in the Inspector.

Page 7: Adding a Trigger to a Unity Animation M2pixelstreetstudios.com/.../06/Adding-a-Trigger-to-a-Unity-Animation-M… · Unity Version: 5.0 Adding the GameObjects In this example we will

  7  

Move the door to the open position for both keyframes To create the close state we will need to add a second animation clip to the door. In the Animation panel, left-click DoorOpen at the top of the panel and choose [Create New Clip].

Click DoorOpen and choose [Create New Clip] Name the clip DoorClose.

Page 8: Adding a Trigger to a Unity Animation M2pixelstreetstudios.com/.../06/Adding-a-Trigger-to-a-Unity-Animation-M… · Unity Version: 5.0 Adding the GameObjects In this example we will

  8  

Name the new animation clip, DoorClose Click the Add Property button and click the plus icon next to Position in the Transform section.

Click the Add Property button, then click Position Notice that if you scrub the timeline, the door remains closed for the entire animation. We now have two door states, DoorOpen and DoorClose. Click the red dot in the upper left corner of the Animation panel to stop the recording.

Click the red dot to stop recording Double-click the Animator Controller (Door) in the Project panel. The Animator panel opens.

Page 9: Adding a Trigger to a Unity Animation M2pixelstreetstudios.com/.../06/Adding-a-Trigger-to-a-Unity-Animation-M… · Unity Version: 5.0 Adding the GameObjects In this example we will

  9  

Double-click the Animator Controller (Door) to open the Animator panel.

Your two animation clips appear in the Animator panel Right-click the DoorClose icon and choose Set as Layer Default State. Now when you play your game, the door will start in the closed position.

Page 10: Adding a Trigger to a Unity Animation M2pixelstreetstudios.com/.../06/Adding-a-Trigger-to-a-Unity-Animation-M… · Unity Version: 5.0 Adding the GameObjects In this example we will

  10  

Click DoorClose, then right-click DoorClose and choose Set as Layer Default State Click the Parameters tab in the Animator panel. This is where we will add a Boolean variable that tells the door to move to the appropriate state.

Click the Parameters tab in the Animator panel Click the plus icon at the top of the panel and choose Bool for type. Name the variable OpenState and press Return.

Page 11: Adding a Trigger to a Unity Animation M2pixelstreetstudios.com/.../06/Adding-a-Trigger-to-a-Unity-Animation-M… · Unity Version: 5.0 Adding the GameObjects In this example we will

  11  

Add a new Boolean variable and name it OpenState

Click the checkbox next to OpenState to activate the variable.

Click the checkbox to activate the variable To create the transitions between the two states, right-click the DoorClose state and choose Make Transition, then click the DoorOpen state to make the connection.

Right-click DoorClose and choose Make Transition, then click DoorOpen Repeat the process the other direction by right-clicking the DoorOpen state and choosing Make Transition. Click the DoorClose state to add the transition.

Page 12: Adding a Trigger to a Unity Animation M2pixelstreetstudios.com/.../06/Adding-a-Trigger-to-a-Unity-Animation-M… · Unity Version: 5.0 Adding the GameObjects In this example we will

  12  

Once the connections are made, you should have lines pointing both directions Click the arrow on the transition line from DoorOpen to DoorClose. In the Inspector, click the plus icon in the Conditions section.

Click the plus icon in the Conditions section to add a condition Set the OpenState condition to false. Deselect the Has Exit Time checkbox.

Page 13: Adding a Trigger to a Unity Animation M2pixelstreetstudios.com/.../06/Adding-a-Trigger-to-a-Unity-Animation-M… · Unity Version: 5.0 Adding the GameObjects In this example we will

  13  

Set OpenState to false and deselect the Has Exit Time checkbox Click the arrow on the transition line from DoorClose to DoorOpen. In the Inspector, click the plus icon in the Conditions section. Set the OpenState condition to true. Deselect the Has Exit Time checkbox.

For this transition, set the OpenState to true and deselect the Has Exit Time checkbox Creating the Script

Page 14: Adding a Trigger to a Unity Animation M2pixelstreetstudios.com/.../06/Adding-a-Trigger-to-a-Unity-Animation-M… · Unity Version: 5.0 Adding the GameObjects In this example we will

  14  

Right-click the Assets folder and choose Create > C# Script.

Create a new C# script Change the script name to SlidingDoor. Double-click the script to launch Mono Develop.

Double-click the script to open the script in Mono Develop Change the script to read as follows: using UnityEngine; using System.Collections; public class SlidingDoor : MonoBehaviour { public Animator doorOpener; void Awake () { doorOpener.SetBool("OpenState", false); }

Page 15: Adding a Trigger to a Unity Animation M2pixelstreetstudios.com/.../06/Adding-a-Trigger-to-a-Unity-Animation-M… · Unity Version: 5.0 Adding the GameObjects In this example we will

  15  

void OnTriggerEnter (Collider ckForPlayer) { if (ckForPlayer.gameObject.tag == "Player") { doorOpener.SetBool ("OpenState", true); } } void OnTriggerExit (Collider ckForPlayer) { if (ckForPlayer.gameObject.tag == "Player") { doorOpener.SetBool ("OpenState", false); } } } Save and close the script and return to your Unity project. Add a first person controller to the scene by choosing Assets > Import Package > Characters. Click Import once the Importing Package panel appears. Open the Standard Assets folder in the Project panel, then open the Characters folder. Open the First Person Controller folder, then click the Prefabs folder. Drag the FPSController to the scene.

Select the FPSController prefab and drag it to the scene Use the move tool to drag it upwards so the entire capsule is above the plane. Use the Rotate tool to rotate the FPS Controller so it faces the door.

Page 16: Adding a Trigger to a Unity Animation M2pixelstreetstudios.com/.../06/Adding-a-Trigger-to-a-Unity-Animation-M… · Unity Version: 5.0 Adding the GameObjects In this example we will

  16  

Position the FPSController capsule above the Ground object You may need to scale the Ground object larger so you can navigate and stay on the plane. In the Hierarchy panel, right-click the Main Camera and choose Delete. This camera is no longer necessary since we have a FPSController camera. With the FPSController active in the Hierarchy panel, open the inspector and notice that the Tag box currently says Untagged.

Select the FPSController in the Hierarchy panel. Notice that it is currently untagged.

Page 17: Adding a Trigger to a Unity Animation M2pixelstreetstudios.com/.../06/Adding-a-Trigger-to-a-Unity-Animation-M… · Unity Version: 5.0 Adding the GameObjects In this example we will

  17  

From the pull-down menu, choose Add Tag.

Choose Add tag from the pull-down menu Click the plus icon in the Tags section and name your new tag, Player. Return to the Tag pull-down menu and set the FPS Controller tag to Player.

Choose Player from the pull-down menu Click the Assets folder in the Project panel and drag the SlidingDoor script to the DoorCollider object in the Hierarchy panel.

Page 18: Adding a Trigger to a Unity Animation M2pixelstreetstudios.com/.../06/Adding-a-Trigger-to-a-Unity-Animation-M… · Unity Version: 5.0 Adding the GameObjects In this example we will

  18  

Drag the SlidingDoor script from the Project panel to the DoorCollider object in the Hierarchy panel Select the DoorCollider object in the Hierarchy panel and open the Inspector. Notice that the Door Opener variable is not connected to the Door Object Animator component.

The Door Opener variable is not connected – None (Animator) Drag the Door object from the Hierarchy panel to the Door Opener box in the Inspector panel. It should now read, Door (Animator).

Page 19: Adding a Trigger to a Unity Animation M2pixelstreetstudios.com/.../06/Adding-a-Trigger-to-a-Unity-Animation-M… · Unity Version: 5.0 Adding the GameObjects In this example we will

  19  

The box now reads, Door (Animator) Press the Play button to test the door. As you enter the DoorCollider object, the door opens. Whenever you exit the sphere the door closes.