In this recipe, we will learn how to animate an object's local position to be able to use the same animated object in multiple locations in the scene. We will create another common gameplay mechanism, automatic door, as an example.
Getting ready
To make an animated door, you should have two objects ready: the Door Frame and the Door. The Door should be a child of the Frame in the Hierarchy. It should be placed in the closed position. We assume that our Door will slide upward when opening. You should also have a player character with the Player tag assigned. The character should use a Rigidbody component to move or have a kinematic Rigidbody component. We will use triggers for our doors, and triggers react to Rigid bodies only. You can also go to the Chapter 02 Working with the animation view\Recipe 04 Animating objects local position - creating automatic doors directory and find
the Example.unity scene there. If you open it, you can find Automatic Door game object in the Hierarchy and a Sheep game object that will work as our player (it has the
Player tag). If you run the game and go near the door, it will open.
How to do it…
To create automatic doors, follow these steps:
Select your Door Frame game object, and add an Animator component to it. To 1. do so, click on the Add Component button in the Inspector and
choose Animator. Make sure to add the component to the Door Frame instead of the Door (the parent instead of the child). Set the Update Mode of the Animator component to Animate Physics.
With the Door Frame game object selected, go to Window | Animation to open 2. the Animation View.
Create a new Animation Clip and call it Closed; this will be our animation for 3. the closed door. To create a new clip, click on the drop-down list below the
playback buttons and choose the Create New Clip option.
Click on the Add Property button and find your Door object in the list (a child of 4. the Door Frame game object).
Unfold the Door game object foldout and find the Transform section. Unfold it 5. and click on the plus icon near the Position property, as shown in the following
screenshot:
Two key frames will be added for the Door game object's local position. We are 6. not going to adjust them (assuming that the Door is in the closed position).
Add another Animation Clip and name it Door Opening.
7.
Click on the Add Property button, choose your door game object, and add 8. the Transform | Position key the same way as in step 5.
Move the second key frame to around the 3 second mark.
9.
Select the Door game object (the child of the Door Frame).
10.
Move it up to the open position. Make sure the record button is active (in the 11. upper-left corner of the Animation View). If you play the animation, it should
move the Door game object up.
Select the last frame of the Door Opening animation (in the Animation View). Go 12. to the Inspector and copy the Door's Transform component. To do so, click on
the small gear icon in the upper right corner of the component and choose the Copy Component option.
Create a new Animation Clip and name it Opened. Add the key frames to
13. the Door child object the same way as in step 5. If your Door object moved, select it, click on the gear icon near the Transform component in the Inspector, and choose Paste Component Values. That will paste our copied Transform properties from the last frame of the Door Opening animation.
Make sure both frames in the animation have the same position.
14.
Create one more Animation Clip (the last one) and name it Door Closing.
15.
Make the animation of the Door game object slide down from open to closed 16. positions. Remember that you can copy the Door's Transform component from
appropriate animation frames and paste them in the animation you are working on.
Preview all the animations with the Animation View. You can switch between 17. animations using the drop-down list below the playback buttons.
If you are happy with your animations, close the Animation View. Select
18. your Door Frame game object and find its Animator component in the Inspector.
Click on the Controller field, which will show you the automatically 19. created Animator Controller in the Project View. Double-click to open it.
Create a bool parameter and call it Open.
20.
Create a loop of transitions between the animation states: Closed | Door 21. Opening | Open | Door Closing | Closed. See the following screenshot:
Select the Closed | Door Opening transition. Add a condition to it, choose 22. the Open parameter, and set the condition to true. Disable the Has Exit Time
option in the transition.
Select the Opened | Door Closing transition. Add a condition to it, choose 23. the Open parameter, and set the condition to false. Disable the Has Exit Time
option in the transition.
Leave the Door Opening | Opened and Door Closing | Closed transitions 24. without a condition. Make sure the Has Exit Time option is enabled in both those
transitions.
Close the Animator Controller.
25.
Select the Door Frame game object in the scene's Hierarchy.
26.
Add a Cube child object to the Door Frame game object. To do so, right-click on 27. the Door Frame game object and choose Create | 3D Object | Cube. This will be
our trigger.
Select the Cube and scale it so that it stands out on both sides of the Door Frame.
28. See the following screenshot for reference:
Remove the Mesh Renderer and Mesh Filter components from the Cube. To do 29. so, click on the small gear icon in the upper-right corner of the given component and choose the Remove Component option. That will leave only the Transform and Box Collider components on the Cube.
Enable the Is Trigger checkbox on the Cube's Box Collider component.
30.
Rename the Cube game object to Trigger.
31.
Select the Door Frame game object and add a Mesh Collider component to it.
32.
Select the Door game object and add a Box Collider component (or a Mesh 33. Collider set to Convex) and a Rigidbody component. Enable the Is Kinematic
option in the Rigidbody component.
Create a new C# script and name it AutomaticDoors.cs. Open it and write the 34. following code:
using UnityEngine;
using System.Collections;
public class AutomaticDoor : MonoBehaviour { parameter stores information about the object which collided with our trigger (entered the trigger).*/
/* This function is called when a Rigidbody exists the trigger (stops colliding with our trigger collider).*/
void OnTriggerExit (Collider other)
Assign the script to your Trigger game object and make sure your character has 35. a Rigidbody component and the Player tag.
Play the game and approach the door; it should open. It should close when you 36. exit the trigger.
How it works…
This recipe illustrates a very important feature of Unity's animation workflow, the
possibility to animate objects in local space. If we animate child game objects, their position, rotation, and scale will be animated in relation to their parent game objects. This makes it possible to create animated game objects, save them as prefabs, and reuse them in our games.
There are a few key elements of this recipe:
Door as a child of Door Frame: We have two objects: an animated, moving Door and a stationary Door Frame. The Door game object is the child of the Door Frame game object in the Hierarchy. If we animate it, an Animator component will be added to the Door Frame game object (the parent), and the Door (the child) will be animated relative to the Door Frame (the parent).
Bool parameter “Open”: This parameter is used by the door Animator Controller to switch between Opened and Closed states. We set it in the script attached to the trigger game object. When player enters the trigger, it sets the Open parameter to true, which tells the Door Animator Controller it should play the Door
Opening animation. When the player exits the trigger, it sets the Open parameter to false, so the Door Animator Controller plays the Door Closing animation.
Animation-driven movement: The Door game object is animated to open (move up) and close (move down), depending on the bool Open parameter. The Door game object has a collider to prevent any Rigid body going through it. It also has a Rigidbody component set to Is Kinematic. All moving colliders should be kinematic Rigid bodies for optimization reasons.
Trigger game object: The Door Frame has also a trigger game object as a child. A trigger is a game object with a collider set to Is Trigger. It recognizes when an object with a Rigidbody component enters or exits the collider and calls the OnTriggerEnter and OnTriggerExit functions, respectively. We set the value of the bool Open parameter in those functions.
See also
The concept of animating game objects in local space will be used multiple times throughout the course of this book. We are going to create an interesting example in the next recipe.