Creating an Elevator Movement In Unity

addam davis
4 min readOct 14, 2021

--

Objective: Create movement for the elevator when it is called!

In the previous tutorial we set up a panel that will call the elevator when the player has the right amount of coins. We now need to create the movement for the elevator and we are going to create a similar movement pattern to the moving platform from before.

To begin, we are going to duplicate the elevator and remove the child objects as well as the mesh renderer, mesh filter, and collider. This object is going to be an empty object holding location data.

Duplicate the elevator object and this time, drag it to the location you want it. Once placed you can then remove all the components and rename the object. Mine is called “Target”

With these placed and ready we can not move onto the coding side of this. Open the elevator script and we need to create a few variables. We need two transform variables, one for each empty object we just created, a speed variable and a bool for if the elevator is moving or not.

We are going to create a public method for the elevator panel to call. Inside this method we are going to create a toggle. A toggle is a bool that changes every time the method is called.

With the toggle set up we can now create a fixed update method. A fixed update method will provide us with a smooth moving elevator because the fixed update is called 60 frames a seconds while the update method is dependent to your computer specs.

Inside the fixed update method we need to check if our bool is true we want to use the vector3 move towards to the target object, else we want the elevator to move towards the origin position.

If we move the player to the the elevator the player will no longer be colliding with the elevator panel so a simple way to fix this is to extend the collider to cover the elevator as well.

Now, when the player is standing on the elevator the can still press the E key and activate the panel again and toggle the elevator movement bool. In the input method on the elevator panel we need to call the elevator toggle method so we need a reference to the elevator on the panel.

Now the elevator will move up and down when called and the player has the right amount of keys. However, the ride is a little bumpy. To smooth the ride out we need to parent the player to the elevator while the player is on the elevator and unparent the player when the exit.

Add a second collider to the elevator. Set the new collider to be a trigger.

Edit the second collider to fit the whole of the elevator space.

We now need to set up an on trigger enter on the elevator class and check for the player. When the player enters the elevators collider we want to set the player’s parent to be the elevator.

Now, finish it off with an on trigger exit and set the parent to null.

Now the player will move smoothly with the elevator. This is all you need to set up a basic elevator. Instead of a bool you could also an array of floor levels to make the elevator more advanced. Don’t be afraid to experiment with your elevator and I’ll see you in the next tutorial!

--

--