Creating an Elevator In Unity

The Platform game changed shape into a more challenging game. This article will be about calling an elevator if we match the requirements to call the elevator. I have actually really had fun. Here’s my daily journey:

First, we want the Player to be able to call the elevator if he is standing in front of the elevator panel and has collected 8 coins. Elevator Panel has a button as a child object, which will change it’s color to green once we call it if we are meeting the requirements. Basically, we want the player to be able to call the elevator within it’s collider. So this step is a clear usage of onTriggerStay method.

Now, the button part. So, the simple solution would be just creating a handle to the button (by using SerializeField or making it public) and manipulate it’s color that way. Well, I preferred to reach it by code and my first attempt was a disaster. Just a quick note, the button is a child object of the Elevator Platform game object.

I have declared a material variable as call button material. And then:

_callButtonMat = GetComponentInChildren<MeshRenderer>().material;

And funny thing happened, the platform turned into green rather than the button :)). Instead of the child object, it actually cached the mesh renderer of himself.

Not bad, eh? For a moment I thought this was a better way to yell the player that he/she is calling the elevator. But still, it is better to be polite, so let’s just not yell.

Rather than trying to solve it using the same way (yeah I am now really confident that there are many ways to solve a problem), I decided on first reaching to the component, and then to its material.

_callButtonMat = transform.GetChild(0).gameObject.GetComponent<MeshRenderer>().material;

Of course, do not forget to null check.

To follow up the collected coins, I have created a public int Coins property (with only get) in the Player, returning private coins variable.

Works like a charm!

Now that our button turned into green, we can add the elevator movement to the game.

Getting The Elevator Moving

Elevator Scene View

Here’s our tiny elevator. To get it moving, we need to first create a script for its behavior. I have created two empty game objects at the locations on both floors and dragged them as Transform variables from the Inspector. The elevator needs to know if the button is pressed, so I have defined a public bool variable to detect if it’s called from the down floor. Once it’s called, we will tell it to move to the corresponding floor by using Vector3.MoveTowards method — just like we have used in the moving platform game object.

And on the Elevator script:

if(isElevatorCalled){transform.position = Vector3.MoveTowards(transform.position, _floor0.position, _speed * Time.deltaTime);}

With this method, the elevator will only move downwards once we held the conditions. To make it also move upwards, I have increased the collider size of the elevator panel to the right; so that we can make it move upwards while we are in the elevator.

Here’s the updated shots from the scripts:

Elevator Panel Script
Elevator Script

Lastly, to make sure to prevent a jiggering effect while we are on the elevator, it is better to parent the player to the elevator while he/she is in it. Add a new box collider to the elevator as trigger and in the elevator script:

And here’s the result:

Unity Developer