Creating an Elevator in Unity
Going over how I implemented a basic elevator in Unity.
Objective: Implement an elevator for my 2.5D game.
Setup
To get set up for today, I went ahead and created the level for this prototype, which includes some elements for mechanics I will be creating in the next few days as well. The picture below shows off the elevator section in particular.
In this photo, the gold spheres represent collectibles, the dark-grey structure is our actual elevator, and the light-gray plate/door with the red button is how we will call the elevator. With all this, we can get started on the functionality.
The Functionality
The first thing I did when creating the elevator was getting the panel working. To do this I simply used OnTriggerStay() to detect when a player is near the panel and to change the color of the red button when a player hits “E”. This is shown below.
Next, I made it a requirement for the player to have a certain amount of coins to be able to use the elevator. I demonstrate this in the GIF below, where the required amount of coins is eight.
Next, I got the elevator to move up and down based on the player hitting the E key. Implementing this required me to remove the OnTriggerStay() function as it was giving me buggy behavior. I instead utilized OnTriggerEnter() and OnTriggerExit() with a bool to keep track if the player was in the area to activate the elevator. Below is the result:
To get all of this working, these were the two main pieces of code I used:
That’s all for today! Thanks for checking and reading today’s article! In the next article, I will create some wall-jumping mechanics.