How to do it...

To create an elevator triggered by player input, follow these steps:

  1. Select the root Elevator object and assign an Animator component to it. Set the Update Mode to Animate Physics.
  2. Open Window | Animation and create a new Animation Clip by clicking on the Create button. Call it ElevatorGoingDown.
  3. Make sure the record button is pressed (in the upper-left corner of the Animation View).
  4. Select your Lift game object and move it to the up position.
  5. 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.
  6. 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.
  7. Animate your Lift game object to go from the down position to the up position.
  8. Create another Animation Clip and call it ElevatorDown. Create a one-second looping animation for the Lift game object in the down position.
  1. Create another Animation Clip and call it ElevatorUp. Create a one-second looping animation for the Lift game object in the up position.
  2. Close the Animation View.
  3. Navigate to the ElevatorGoingUp and ElevatorGoingDown assets in the Project View. Select them and uncheck the Loop Time option in the Inspector.
  4. Select the Elevator game object in the Hierarchy and find its Animator component in the Inspector.
  5. Double-click on the Controller field to open the automatically created Animator Controller asset.
  6. Create a new trigger parameter and call it Move.
  7. Create a loop of transitions, as shown in the following screenshot:
  1. Make sure the ElevatorDown state is the default one. If not, right-click on it and choose the Set as Layer Default State option.
  2. 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.
  3. The ElevatorGoingDown | ElevatorDown and ElevatorGoingUp | ElevatorUp transitions should have the Has Exit Time option set to true and should have no additional conditions.
  1. 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"); 
             } 
 
         } 
     } 
  1. Assign the Elevator.cs script to the Elevator game object.
  2. Select the ElevatorFrame game object and add a Mesh Collider component to it.
  3. 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:
  1. Remove the Mesh Renderer and Mesh Filter components from the cubes, leaving just the Box Collider components. Give the cubes proper names, for example, FloorColliderLeftCollider, and RightCollider.
  2. Select the Lift object again. Add a Rigidbody component to it and set it to Is Kinematic.
  3. 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.
  4. 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.
  5. Run the game and press the E button on the keyboard to trigger the elevator.
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.144.30.236