How to add an elevator to a platformer in Unity

Let’s go over how to add an elevator with a call button to the platformer project.

The elevator will borrow some of the behaviour from the moving platform script from this article.

Create a script called elevator and attach to the elevator object:

Open up the Elevator script. Add these variables:
-Serialized Transform array for the waypoints
-A float for the speed of the elevator
-Two serialized GameObjects, one for the button in the elevator and another for the light on the elevator panel
-Vector3 for the target the elevator should move to
-Player to reference the player object

Set the start target for the elevator and initialize the Player in the Start function:

In FixedUpdate check if the elevator has been called and start moving it to the target. When it has reach the target, set _called to false to stop it moving and also turn the colour of the button and the panel light to red:

Add a public function called CallElevator which will set _called to true:

Just like with the moving platform script add an OnTriggerEnter and OnTriggerExit function to parent or unparent the player to the platform:

Add a function called CheckForInput which will check the distance between the player and the button in the elevator. If the distance is close enough and E is pressed down, call the CallElevator function. Call the CheckForInput function from the start function:

Drag the waypoints and game objects onto the script on the Inspector and set the speed:

Create another script for the elevator panel called ElevatorPanel and attach to the game object:

Open up the ElevatorPanel script. Add these variables:
-Two serialized GameObjects, one for the button in the elevator and another for the light on the elevator panel
-Serialized int for the score the player needs to activate the elevator
-Serialized Elevator for the elevator this panel will activate
-Player to reference the player object

Initialize the player in the Start function. Create a function called CheckForInput. This works similarly as the function with the same name on the elevator script. Call CheckForInput from the Update function:

Lastly, drag the objects onto the script on the Inspector and set the amount of points needed to activate the elevator:

Good luck!

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store