Time to Going Up!- Game Dev Series 83

Objective: create a functional elevator with calling panel.

Today’s article, we will create an elevator with calling panel.
Elevator in games would be more like a rest stop to me. It usually about level switching, or a saving point.

To create an elevator, it will contain 2 parts:

  • Panel control
  • Elevator movement

Press it on panel

The first thing to do on panel is to create a script and attach to it. Inside the script, we will get the reference of panel button.

We need to make the color of panel light turns green when Player is inside the trigger collider of panel. To do this, we can create OnTriggerStay(). The code would proceed if target is staying in the collider.

While Player is inside the collider, if Player pressed “E”, the panel light would turn green and called the elevator. Turning the color of light is simple, we can just get the material of button and turn it into green.

We can add another condition in here. Make the Player can only pressed the panel button if it has more than 8 coins.
To do this part, we will it to receive the data of coin counts from Player script.

Open Player script and create a new method to return the coin integer.

Then set the reference of Player script in ElevatorPanel script and get this method inside the if statement that detect pressing E key.

Test it to see if the panel light works well.

Moving elevator

Now we can try to call down the elevator. First, create 2 empty gameobjects and set their position as the moving target of elevator.

Then create a new script and attach it to elevator.

Same as we create the moving platform, use FixedUpdate() to move elevator. Use Vector3.MoveTowards() in it. However, we need to set the direction.

Use a bool as switch to identify the moving direction. And switch this bool in a public method to allow panel script to call it.

In this method, we use _goingDown = !_goingDown to make the value switched.

And use the bool in FixedUpdate().

Now we can call elevator by calling the public method.

Back to panel script, create a new variable of Elevator script and call the reference in Start(). Then, add one more line after switching the panel light to green.

Stretching the trigger collider of panel to allow Player call the elevator to go up when inside it.

And our elevator should be ready now!

Bonus: fix the jiggling issue when going up

When going up with elevator, the Player would jiggling all the time. This would be easy to fix by using the same way of fixing the issue of moving platform: set the moving object as parent of Player.

Add a new box collider to elevator and stretch it to cover the whole walking area.

Don’t forget to set it as trigger.

Then use OnTriggerEnter and OnTriggerExit to set the parent to Player.

All done! Now you got a smooth working elevator!

--

--

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