Platformer: Elevator Movement

Ryan McCoach
3 min readNov 8, 2021

Intro

In this article, we will cover having the elevator move down when it is called and when the player steps onto the elevator it will move up to the next level.

Setting Up The Elevator

The elevator is made up of 3 basic cubes and will have a Box Collider with Is Trigger true and a Rigidbody with Use Gravity to false. This is so we can detect when the player steps onto the elevator.

We will also need to create two empty gameobjects for the start position and end position. This is all stored in an empty gameobject, Elevator_Container, to keep everything neat and organized.

The Code

The first thing is to being able to access the new elevator script from the elevator panel script. The reason being the elevator panel has the button that detects when the player presses meaning it has been called to move down. We declared an Elevator class, assign it using GameObject.Fin, and complete a null check.

When the player calls for the elevator, we access the elevator script and call the CallElevator method.

In the Elevator script, we declared these variables which I will go over how each will be implemented.

The first is a bool that keeps track of if the elevator should be moving down and is set to true when Call Elevator method is called from the elevator panel script.

In a fixed update, if the moving down is true, we set the position of the elevator to move towards the transform of the End game object at the speed of variable. If moving down is false, meaning it has reached the bottom floor, we reverse it by setting the transform to move towards the transform of the start game object.

Also, when moving down is true, we change the elevator button changed to green and when it is moving up, we set it to red.

The last thing is having the player be coupled with the elevator to prevent it from jittering. We use the OnTriggerEnter for when the player enters the BoxCollider on the elevator. Once this happens we set the player’s to be a child of the elevator and set the moving down bool to false, since we want the elevator to now move up.

When the player exits the elevator, we use OnTriggerExit to exit the player from being a child of the elevator by setting it to null.

Conclusion

It was extremely fun to figure out how to create buttons to create moving platforms and I will want to return to this in hopes to make it more modular. I feel elevators are essential in platformers along with button presses being used in puzzles for the player to solve. Until next time…

Cheers!

--

--