Unity Guide
Implementing an elevator | Unity
A quick guide about how to implement an elevator in Unity
Objective: Implement an elevator to carry the player in a platformer game with Unity.
In the last post I covered how to create a life system with Unity. Now, it’s time to implement an elevator to allow the player to move to a new area in our platformer game.
Current level
In order to cover some of the next features of our platformer game we’ll be using this scene in Unity. The features to implement after the elevator are wall-jumping and pushing objects to complete a puzzle.
So, let’s take a look at the components that we’re going to use to implement the elevator. We have an elevator panel with a red gameobject to represent a light that will change color when we call the elevator that’s above:
Implementing the elevator
In order to implement the elevator behavior we’ll use this elevator, which consists in 3 cubes with a normal box collider (to handle the interactions with the character controller of the player):
Now, to define the waypoints that define the path of the elevator, let’s create the next empty gameobjects:
- Elevator container
This empty gameobject will work as the parent to store the elevator and the respective waypoints to define the path to be followed.
- Origin
This empty gameobject will define the initial waypoint of the path.
- Goal
This empty gameobject will define the last waypoint of the path.
Once the gameobjects are created, let’s modify the respective position of each waypoint to make sure that they’re where we want the elevator to stop:
Then, let’s create a new script and attach it to the elevator:
Implementing movement
Once created, let’s open the script and create the next variables to handle the elevator behavior:
- Light
This variable will store a reference to the Mesh Renderer component of the red light at the elevator panel so that we can change its color according to the elevator movement.
- Speed
This variable will store the value of the speed that the elevator moves to.
- Origin & goal
This variable will store a reference to the Transform components of the empty gameobjects that define the path of the elevator.
- On Origin
This variable will indicate if the elevator is at the origin or at the goal. Currently it’s set to false by default as the elevator is at the goal (above).
- Moving
This variable will define if the elevator should be moving towards an objective or not.
Now, in order to handle the movement (and also avoid a weird behavior when the player is on the elevator), let’s use the FixedUpdate method. In this method we’ll:
- Check if the elevator should be moving
- → Check if the elevator is at the origin or not
- →→ Check if the elevator has reached the position of the respective objective
- →→→ If that’s true, let’s stop movement and indicate if the elevator is at the origin or not
- →→→ If that’s false, let’s move the elevator towards the objective
The MoveTo method receives a Vector3 parameter value that is being used within the Vector3.MoveTowards method to define the new position of the elevator:
Finally, to connect the elevator panel with the elevator, let’s create a new public method that will tell the elevator to move and the color of the light to change if the elevator has stopped and it’s not at the origin:
Smooth ascent
And now, in order to lift the player smoothly, let’s attach a new collider to the elevator (but this time let’s enable the Is Trigger property) that will merge it with the player:
Then, in order to handle the respective trigger, let’s use the OnTriggerEnter method to do this:
- Check if the Player tag gets identified
- → Check if the elevator is stopped and move it if that’s the case.
- Check if the elevator is at the origin
- → If that’s true, change the light color to red
- → If that’s false, change the light color to green
- Set the elevator to be the parent of the player so that it moves with it smoothly.
Finally, let’s use the OnTriggerExit method to disjoin the player from the elevator when it exits the respective collider:
Now, let’s modify the respective values through the inspector so that the elevator script works as expected:
Calling the elevator
Next, in order to be able to call the elevator by using the elevator panel, we’ll use a box collider with the Is Trigger property enabled:
Now, let’s create a new script for the elevator panel:
Once created, let’s create the next variables to handle its behavior:
- Coins required
This variable will store the value that will determine the number of coins that the player should have in order to be able to call the elevator.
- Player ref
This variable will store a reference to the player in order to check how many coins it has collected.
- In range
This variable will determine if the player is at the specific range to be able to call the elevator.
- Elevator
This variable will store a reference to the elevator so that we’re able to call it using the public method that we created in the elevator script.
Then, let’s use the OnTriggerEnter method to obtain the Player reference and indicate that the player is in range to call the elevator:
Also, let’s indicate that the player isn’t in range by using the OnTriggerExit method:
And now, using the Update method, let’s check if the player is in range and if the E key is pressed. If that’s the case, let’s check if the player has the required coins in order to call the elevator using the public method from the Elevator class:
Finally, let’s modify the respective values through the inspector in order for our script to work as expected:
If we run the game in Unity, we’ll see that the elevator and the elevator panel work as expected:
And that’s it, we implemented an elevator for our platformer game with Unity! :D. I’ll see you in the next post, where I’ll be showing how to implement a wall jump with Unity.