Making an Elevator

Kinga Osińska
3 min readJul 12, 2021

Today we will start making an elevator system.

The elevator system will consist two objects, the elevator panel and the elevator. The Player will need to gather enough coins to call the elevator using the “E” key. When on the elevator Player will press again the “E” key to go up.

Create the elevator panel. The elevator panel consist two GameObjects, panel and button that will turn green when the elevator is called. The panel will contain a Box Collider with isTrigger checked.

Create an ElevatorPanel script and attach it to the GameObject. Inside, we will create two variables, _button and _requiredCoins. Assign the button in the Inspector panel.

In the Player script create a new method that will return the number of coins that the Player has.

We will call OnTriggerStay to check if the Player is inside the collider, and if he has the required number of coins. If he is and he press the “E” key, we will change the color of the button to green.

Next, create a new script called Elevator. We will need to create few new variables inside. _goingDown variable will tell us if the elevator should move up or down. the _origin and _target will tell us where the elevator should move to.

Duplicate the elevator and move it where you want it to move to. Make another copy and the position on the elevator.

Remove all the component from those two GameObjects, so they will become empty GameObjects. Name the Origin and Target. Assign them in the Inspector in the Elevator script.

In the FixedUpdate() method we will move the elevator depending on the bool variable. We will use Vector3.MoveTowards() method to move.

Create a new method that will change the _goingDown variable.

Go back to the ElevatorPanel script. We will need to access the Elevator script. We will also add a new variable called _elevatorCalled.

When we press the “E” key the elevator will be called down and the button will turn green. If we go into the elevator and press the “E” key again, the elevator will go up and the button will become red.

The last thing we need to do is to fix the elevator jitter. When the Player is inside the elevator when it’s moving it is jittering. Like with moving platforms, we need to assign the Player GameObject to be the child of the elevator when he steps inside.

--

--