Geek Culture
Published in

Geek Culture

Create an Elevator In Unity

This article will show how to create an elevator system that the Player will interact with and ride to the next floor in the scene.

The elevator system will consist of two objects, the elevator panel and the elevator itself. The panel will require the Player to have collected enough coins to interact with it and call down the elevator. Once the elevator has reached the bottom, the Player steps aboard and then interacts with it to have the elevator move back to the top.

Player

Before making the elevator panel, we need to add a method on the Player script to be called by the panel to return the number of coins the Player has collected.

Elevator Panel

The elevator panel has a Box Collider set to be a Trigger used to detect when the Player is standing inside it. The panel also has a call button child that will change Color when the panel is interacted with to give the Player visual feedback.

The ElevatorPanel script needs a few variables and an Action Delegate to call the elevator with the given ID number.

  1. One for the required number of coins.
  2. The Mesh Renderer for the call button to change the Color.
  3. The Color to change to.
  4. The original Color.
  5. The ID number of the elevator this panel will call.
  6. A bool to know if the Player called the elevator already.

In Start, we get the starting Color of the call button.

We use OnTriggerStay to detect if the Player is inside the panel’s Collider. If the Player presses the ‘E’ key in the Collider, we check the number of coins collected. If it was enough coins, then we change the Color of the call button, swap the elevator called bool, and invoke the Action to call down the elevator with the passed ID number.

If we enter Play Mode, the elevator panel requires eight coins to activate, and the call button changes Color when the Player presses the ‘E’ key inside the Collider.

Elevator

The elevator is made of three cubes to create the appearance of an elevator cut to reveal the inside. On the elevator are two Box Colliders. One is used to act as the floor so the Player will not fall through. The other is a Trigger that will detect if the Player is entering or exiting the Collider and then parent or unparent to the elevator. We are doing this so the Player will move smoothly with the elevator.

The Elevator script is going to work similarly to the MovingPlatform script from a previous article. For it, we need a few variables.

  1. The ID number for the elevator to know if the panel called it.
  2. The speed the elevator will move at between the two points.
  3. A reference for the bottom point.
  4. A reference for the top point.
  5. A bool to know if the elevator is moving up or down.

In FixedUpdate, we move the elevator towards the bottom point at the set speed if the bool is true and towards the top point if the bool is false. When the elevator reaches either end, it stops moving until the bool is flipped.

When the Player enters the Trigger Collider of the elevator, it is set to be a child of the elevator, and when the Player exits, it is unparented.

This method checks the passed-in ID to see if it is the same as this elevator’s ID and flip the moving down bool to tell the elevator to move. Then we subscribe and unsubscribe the method to the ElevatorPanel’s Action Delegate.

When the elevator panel is activated, it calls the elevator and changes the bool, causing it to move.

--

--

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
Kyle W. Powers

Kyle W. Powers

Unity Developer, Software Engineer, Game Developer