In this last recipe, we will use player input to create a simple elevator.
Getting ready
Before we start, you should prepare an elevator model to contain the objects: the root object called Elevator, the Lift object (this one will be animated), and the ElevatorFrame object, which will work as decoration. The Lift and ElevatorFrame objects should be children of the empty Elevator object. You can also use the example Unity project provided and go to the Chapter 02 Working with the animation view\Recipe 07 Creating an elevator triggered by player input directory. You will find
an Example.unity scene there with the Elevator game object and its children Lift and ElevatorFrame objects. The Elevator is already animated in the example assets.
How to do it…
To create an elevator triggered by player input, follow these steps:
Select the root Elevator object and assign an Animator component to it. Set 1. the Update Mode to Animate Physics.
Open Window | Animation and create a new Animation Clip by clicking on 2. the Create button. Call it ElevatorGoingDown.
Make sure the record button is pressed (in the upper-left corner of the Animation 3. View).
Select your Lift game object and move it to the up position.
4.
Adjust the timeline a few seconds forward and move the Lift game object to the 5. down position. Your Lift game object should animate going from the maximum
up position to the minimum down position.
Create another Animation Clip by selecting the Create New Clip option from the 6. animations drop-down list (you can find it below the playback buttons). Call
it ElevatorGoingUp.
Animate your Lift game object to go from the down position to the up position.
7.
Create another Animation Clip and call it ElevatorDown. Create a one-second 8. looping animation for the Lift game object in the down position.
Create another Animation Clip and call it ElevatorUp. Create a one-second 9. looping animation for the Lift game object in the up position.
Close the Animation View.
10.
Navigate to the ElevatorGoingUp and ElevatorGoingDown assets in the Project 11. View. Select them and uncheck the Loop Time option in the Inspector.
Select the Elevator game object in the Hierarchy and find its Animator 12. component in the Inspector.
Double-click on the Controller field to open the automatically created Animator 13. Controller asset.
Create a new trigger parameter and call it Move.
14.
Create a loop of transitions, as shown in the following screenshot:
15.
Make sure the ElevatorDown state is the default one. If not, right-click on it and 16. choose the Set as Layer Default State option.
The ElevatorDown | ElevatorGoingUp and ElevatorUp | ElevatorGoingDown 17. transitions should both have the Move trigger set as the condition and the Has
Exit option set to false.
The ElevatorGoingDown | ElevatorDown and ElevatorGoingUp | ElevatorUp 18. transitions should have the Has Exit Time option set to true and should have no
additional conditions.
Create a new C# script and call it Elevator.cs. Write the following code:
19.
using UnityEngine;
using System.Collections;
public class Elevator : MonoBehaviour {
// Update is called once per frame void Update ()
{
/*When the player presses the E key, we are setting the Move trigger on the Animator component. We are assuming the Animator component is present on the game object our script is attached to*/
if (Input.GetKeyDown (KeyCode.E)) {
GetComponent<Animator> ().SetTrigger ("Move");
} } }
Assign the Elevator.cs script to the Elevator game object.
20.
Select the ElevatorFrame game object and add a Mesh Collider component to it.
21.
Select the Lift game object and add three Cube game objects to it by choosing 22. the 3D Object | Cube from the right-click menu. Use the cubes to encapsulate the
floor and two barriers of the Lift (the number of the cubes may vary in your particular case). See the following screenshot:
Remove the Mesh Renderer and Mesh Filter components from the cubes, 23. leaving just the Box Collider components. Give the cubes proper names, for
example, FloorCollider, LeftCollider, and RightCollider.
Select the Lift object again. Add a Rigidbody component to it and set it to Is 24. Kinematic.
Add the Platform.cs script to the Lift game object. You can find the script in 25. the Animating an object's world position – creating a moving platform recipe, the How
to do it section, step 13.
This simple elevator will work for characters using Rigidbody components. You 26. can use the ThirdPersonCharacter prefab from Unity's Standard Assets or use
the one in the Unity example project provided.
Run the game and press the E button on the keyboard to trigger the elevator.
27.
How it works…
This recipe has a few key elements that make it work:
Animated Lift game object: The Lift game object uses animations to go from the up to the down position.
Lift as a child game object: The Lift game object is a child of the Elevator game object, which makes it possible to copy the Elevator and use it in multiple places in the scene.
Platform.cs script: We are using the same Platform.cs script as in the
Animating an object's world position – creating a moving platform recipe. This script parents the character to the Lift. This way the character is forced to move with the Lift, which prevents it from jittering (you can try removing the Platform.cs script from the Lift game object and going down in the Elevator: the character will jump up and down).
Player input: We are using player input to set the Move trigger parameter in the Animator Controller of the Elevator. That plays the animation
(ElevatorGoingUp or ElevatorGoingDown, depending on the state in which the Elevator is currently in).
There's more…
Right now, the Elevator will move after every player input, regardless of whether the player is standing in the Lift or not. You can use a Box Collider component set to Is Trigger and write a script to check whether the player is in the Lift, and only then allow the player to use the input. Here is an example LiftCheck.cs script that would do it (you can find the script in the example Unity project provided in the Chapter 02 Working with the animation view\Recipe 07 Creating an elevator triggered by player input\Scripts directory):
using UnityEngine;
using System.Collections;
public class LiftCheck : MonoBehaviour {
elevatorScript = transform.root.GetComponent<Elevator> ();
}
if (other.gameObject.CompareTag ("Player")) { /*We disable the Elevator script (and , disable the input)*/
elevatorScript.enabled = false;
} } } }