Building an Elevator — Ups and Downs

Jack Leavey
4 min readOct 27, 2022

--

Elevators are simple machines that can diversify your level!

Starting with the elevator panel, I created a new c# script (ElevatorPanel).

Inside this script I created references to the player, the amount of coins the player needs to have collected to progress, the meshrenderer of the button, and the elevator itself.

Elevator Panel variables!

_player is set to the player character, allowing this script to contact the player controller.

_requiredCoins is a check to make sure the player has collected all previous coins before progressing. This is compared to the player.cs script’s _coins variable that tracks the total coins collected.

_buttonColor allows us the change the visuals of the elevator button, letting the player know it works!

When the button is green, the elevator is on the way!

Finally, the _elevator is a reference to the elevator gameobject itself; we need this to communicate with the elevator.cs script (to be covered later in this article).

Using OnTriggerStay, we check if the player is colliding with a collider, and if they are, we check for an input and if they are carrying enough coins.

Reference from the Player.cs Script

If that is true, we swap the button’s color and we call the CallElevator function from the Elevator object.

Now for the elevator itself:

This is a different c# script attached to the elevator gameobject (Elevator.cs)

We need a few variables here as well:

isGoingDown is a boolean tracking which direction the elevator is currently navigating to (up or down).

_origin and _target are two transforms (locations) we need to set to the two possible stopping locations of the elevator itself.

Cycling between the two saved transforms.

finally, _speed is just a float to control how fast the elevator ascends or descends.

With the variables set, we used FixedUpdate() to manage the movement of the elevator itself.

When isGoingDown is true, we move towards the _target.position location (the bottom floor).

When isGoingDown is false, we move towards the _origin.postion location (the top floor).

The logic to change the boolean is quite simple:

This line of code flips the boolean.

This function is called from the elevator panel, so now when the player interacts with the panel it will flip the bool, allowing the player to control when the elevator comes down or goes back up.

Finally, we need to add the ability for the player to make the elevator go back up when they are on it.

If the player is colliding with the elevator collider and they press E, isGoingDown is false, making the elevator move back up!
Make sure the elevator collider can actually overlap the player to read the OnTrigger functions!

Finally, in order to avoid stuttering while on the elevator, we need to parent the player to the elevator while they are colliding with this collider. This is identical to how I did it in my moving platforms article, but here is the code again;

When on the elevator, the player is made a child of the elevator. When leaving the elevator, the player is no longer a child.

This simple elevator system can be expanded upon to an infinite number of floors, speeds, functions, and more. This modular baseline allows for a reusable concept that can be used to prototype both 2D and 3D games moving forward!

--

--

Jack Leavey
Jack Leavey

Written by Jack Leavey

I am a software engineer with years of experience branching into game development, specifically in Unity. Follow along for guides on creating game mechanics!

No responses yet