Elevators In Unity

Simon Leen
5 min readAug 22, 2021

Objective: Create an Elevator In Unity

Moving on from the Player lives, we move back into the level logic. As shown in the above image we have changed the platforms around and added in a few more bits which will get to as the posts go on. For now we will focus on the Elevator. The scene is slightly hard to see so I will add a background image, color the coins a yellow/gold, the normal platform floors a black and the Player a purple.

In the above image the Player has reached the elevator call switch. We need to implement a way for the Player to activate this which brings the elevator down to the Player.

We can see above that there is a collider around the elevator switch panel. This will be used with an OnTriggerStay and nested conditional if statement to see if the Player has hit the interact button (E). When the Player is in range and hits the button the color on the panel call button will change to blue. We will change it later to green when the elevator arrives.

Next we will restrict the elevator call to only when the Player has collected all 8 coins available before the elevator. To do this we need to get the Players coins from the Player. We create a public int Coins method that returns the coins in the Player script. Back in the elevator switch script we get a handle to the Player using the other GetComponent and null checking to make sure we have it. We create a private int for required coins and then in the if Player interacts(pressed E), we add a has required coins(Player coins greater or equal to required coins).

You will notice the elevator.CallElevator in the above snippet. For this a new elevator script was created and attached to the elevator component. A bool for if the elevator goes down was created and set to false. Two serialized private transforms for origin and target of elevator were created and assigned to empty game objects set in position in the scene with no mesh renderers to keep them invisable during game play. A serialized private MeshRenderer was created and assigned to the callButton just like when when the Player interacts with the elevator panel.

A public CallElevator method was created as called in the above snippet which sets the elevator down bool to true. A FixedUpdate was used to check a conditional if down was true before moving the elevator from the origin transform towards the target transform. Another conditional if was used to see when the elevator reached the target and then set the call button material color to green.

In setting up the logic to have the elevator go back up, I decided to change the code around a bit. I renamed the method CallElevator to GoingDown and it now accepts a bool. The collider on the elevator switch was extended to cover the elevator floor that the Player would stand on.

As mentioned earlier I decided that the call button switch would be set to blue and that later I would set it to green. I’ve added logic to the call button/elevator switch panel so that if the elevator is at the top floor/origin position the button will be red. At the bottom floor/target position the light will be green and moving either up or down the light will be blue. The logic also stops the Player using or calling the elevator between states. I think as kids we all liked to press the elevator button and watch it go up and down. Now you can in the game just as a fun bonus.

We have some jittering going on when the Player is on the elevator similar to the moving platform issue. To fix this we start by adding a new collider to the elevator component and scaling to take up most of the area the Player can be. We will set as IsTrigger so OnTriggerEnter can make the Player a child of the elevator while on the elevator only. We also add a Rigidbody to the cube and set Use Gravity to off. Once its in place and set, we turn off the MeshRenderer so the cube can’t be seen but it can be used.

We then go into the elevator script and create an OnTriggerEnter and OnTriggerExit method that set the Players parent to the elevator while on the elevator and set the parent to null. This gives a bit of weirdness when the elevator reaches the top shown below but this is an easy fix by setting constraints, freezing the rotation along with the X and Z position of the elevator.

--

--