Creating an Elevator in C# and Unity
Right now I am working on a platform game. And an elevator is another basic feature. It will also be useful in many other situations.
So how do we create an elevator?
Essentially an elevator consists of a game object with a start and end position. It moves between those position when it is called.
So let’s look at this in a prototype environment. I have two platforms. At the lower one a there is a panel with a button. The elevator is on the upper platform. I want it to move when the player is near the bottom panel and presses the button, by pressing “E”.
We need the following objects:
All of them are just simple cubes for now.
The elevator panel
The elevator panel has a box collider set to “IsTrigger” so that we can detect if the player is nearby. Only then can he press “e” to call the elevator. To give a better feedback, the button will also switch to green, when the player activates it with “e”.
Let’s create a new script called “ElevatorPanel”. It needs two variables:
The _callButton allows us to change the material color. In the inspector we associate the Call_Button with the variable.
The bool _atElevator tracks if the player is near the elevator panel. An OnTriggerEnter and OnTriggerExit works well.
As for the main part, we can use the update method:
And it works. Now to our next step… getting that elevator down.
First we need a new script called “Elevator”.
Assuming that we have a game object for the elevator, we now also need two positions, the one at the start and the one at the bottom. The easiest way to do this, is to duplicate the elevator in hierarchy, remove the mesh renderer, rigidbody and script. Save one as “Origin”. Then adjust a second copy so that it matches the bottom position and save it as “Target” or anything you like.
In the script we need to store them in three variables: a bool to check where the elevator is. A transform to store our origin and target positions and a speed for the elevator.
I used a SerializeField on the transform variables, so that I can associate it in the inspector.
Back in the Elevator Panel script, we can now add a new line of code.
When the player is detected at the panel and the e is pressed then we can call the elevator. We are going to add the CallElevator() method as well as a FixedUpdate method in the elevator script as follows:
The =! creates a nice switch. Whenever it is called, it reverses the value of the bool _elevatorDown. Thus when we hit it the first time, the bool is set to true, moving the elevator down. When we hit it the second time, it is set to false, moving the elevator back up to the original position.
Everything works now. However the elevator is a bit jittery. To smoothen the movement we can associate the player to the elevator while he is on it (make it a child of the elevator). We need to add an additional trigger to the elevator, for example a second rigidbody with “is trigger” active and then OnTriggerEnter and OnTriggerExit will allow us to switch the association:
And that’s it… our simple elevator takes the player up to the next platform.