Elevators in Unity
Objective: Create an elevator which can be summoned by the player to go up and down if they have 8 coins, with a light indicating elevator status.
The hierarchy window is as shown below. The key features for an elevator are the Lift, which contains the Elevator and two destination points, and the Elevator_Panel, which has the Call_Button.
The Elevator_Panel is a cuboid with a box colliders with a trigger, and ElevatorPanel C# script components, see below. Note the is trigger box collider is large enough and positioned to detect the Player standing in front of the panel. A rigidbody is not needed as the Character Controller physics on the Player is used.
The Call_Button is a child of the Elevator_Panel with the components below, notably the mesh renderer and its associated material.
The ElevatorPanel script on the Elevator_Panel requires several variables shown below. The Elevator object is assigned in void Start(), as is the MeshRenderer of the Call_Button, both are null checked.
Void update checks every frame to see if the E key is pressed while the player is standing next to the panel with the required coins. If all these are true it accesses the CallElevator method on the Elevator script.
The _atElevator bool is set to true if the player enters the box collider trigger using the OnTriggerEnter method below. The _coinsCollected by the player is also retrieved from other using GetComponent to call the CoinCount method on the Player script .
The CointCount method on the Player script is shown below, and returns the number of _coins collected by the player.
If the player leaves the trigger an OnTriggerExit method sets the _atElevator bool to false.
Finally a public void ButtonPressed() method is used to toggle the color of the button between red and green and is called from the Elevator script. The Elevator is a cuboid with a two box colliders, one with a trigger, and Elevator C# script components, see below. Note the is trigger box collider is large enough and positioned to detect the Player standing on the elevator.
The Elevator script on Elevator requires several variables to store the _pointsA & B between which the elevator moves, its current _target, its _speed, a bool to know if the Player is _onElevator, and _elevatorPanel to provide access to the ElevatorPanel script. The_elevatorPanel is assigned in void Start(), and _target is set to _pointA.
Every frame void update checks to see if the Player is _onElevator and has pressed the E key, in which case it calls the CallElevator method to set the elevator destination depending on its current location. The _elevatorPanel.ButtonPressed function changes the the color of the Call_Button on the Elevator_Panel if the elevator is not in route.
A void FixedUpdate method is used to move the Elevator to the current _target using Vector3.MoveTowards. Once it reaches the _target, the _target is set to null until the player activates it.
Similar to the ElevatorPanel script, OnTriggerEnter and Exit are used in the Elevator script to detect when the Player, i.e. other, is _onElevator, but also control the parent of the Player. To provide smooth animation the Player needs to be a child of the Elevator when the elevator is moving, but not while off the elevator.
This code in the Player, Elevator, and ElevatorPanel scripts controls elevator function providing smooth animation.