To create an elevator triggered by player input, follow these steps:
- Select the root Elevator object and assign an Animator component to it. Set the Update Mode to Animate Physics.
- Open Window | Animation and create a new Animation Clip by clicking on the Create button. Call it ElevatorGoingDown.
- Make sure the record button is pressed (in the upper-left corner of the Animation View).
- Select your Lift game object and move it to the up position.
- Adjust the timeline a few seconds forward and move the Lift game object to the 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 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.
- Create another Animation Clip and call it ElevatorDown. Create a one-second looping animation for the Lift game object in the down position.
- Create another Animation Clip and call it ElevatorUp. Create a one-second looping animation for the Lift game object in the up position.
- Close the Animation View.
- Navigate to the ElevatorGoingUp and ElevatorGoingDown assets in the Project View. Select them and uncheck the Loop Time option in the Inspector.
- Select the Elevator game object in the Hierarchy and find its Animator component in the Inspector.
- Double-click on the Controller field to open the automatically created Animator Controller asset.
- Create a new trigger parameter and call it Move.
- Create a loop of transitions, as shown in the following screenshot:
- Make sure the ElevatorDown state is the default one. If not, right-click on it and choose the Set as Layer Default State option.
- The ElevatorDown | ElevatorGoingUp and ElevatorUp | ElevatorGoingDown 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 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:
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.
- Select the ElevatorFrame game object and add a Mesh Collider component to it.
- Select the Lift game object and add three Cube game objects to it by choosing 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, 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 Kinematic.
- Add the Platform.cs script to the Lift game object. You can find the script in 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 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.