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 viewRecipe 07 Creating an elevator triggered by player inputScripts directory):

     using UnityEngine; 
     using System.Collections; 
 
     public class LiftCheck : MonoBehaviour 
     { 
         Elevator elevatorScript; 
         // Use this for initialization 
         void Start () 
         { 
             /*We try to find the Elevator script on the root 
             transform (we are assuming this is the Elevator game 
             object)*/ 
         elevatorScript = transform.root.GetComponent<Elevator> (); 
         } 
     
         // This function is called when our character 
         enters the trigger 
         void OnTriggerEnter (Collider other) 
         { 
             /*We check if we've found the Elevator script*/ 
             if (elevatorScript != null) { 
                 /*We check if the object which entered 
                 the trigger is the Player*/ 
                 if (other.gameObject.CompareTag ("Player")) { 
                     /*We enable the Elevator script 
                     (and enable the input)*/ 
                     elevatorScript.enabled = true; 
                 } 
             } 
         } 
     
         // This function is called when our character 
         exits the trigger 
         void OnTriggerExit (Collider other) 
         { 
             /*We check if we've found the Elevator script*/ 
             if (elevatorScript != null) { 
                 /*We check if the object which exited the 
                 trigger is the Player*/ 
                 if (other.gameObject.CompareTag ("Player")) { 
                     /*We disable the Elevator script (and , 
                     disable the input)*/ 
                     elevatorScript.enabled = false; 
                 } 
             } 
         } 
     } 
..................Content has been hidden....................

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