Day 81 — Creating an Elevator in Unity
Hey and welcome!
Before we move onto adding in the assets for the game and exploring animation and generally aesthetically pleasing things it’s time to add a bit more interactivity into the game starting with an Elevator the player can hop in to!
As you can see from above I’ve got a full scene to work with here, that green platform is a regular moving platform that moves up and down, however with the elevator I only want it to move when the player hits this button:
To start things off we’re going to make the button change colour from red to green when the player is standing near the panel so that they have a visual cue that the elevator is on its way. If you add in the following code you can get something similar:
public MeshRenderer callButton;private void OnTriggerStay(Collider other)
{
if (other.tag == "Player")
{
if (Input.GetKeyDown(KeyCode.E))
{
callButton.material.color = Color.green;
}
}
}
There’s nothing fancy with the panel here as it’s just a cube object with a separate sphere object as a child inside with a red material on it which we’re calling the call button. This button is what you want to drag in for the MeshRenderer that the code looks for and since we have a reference to it we simply change its material color to green when the E key is pressed.
The new thing happening here though is OnTriggerStay() this one checks the collision continuously while the player is within the collider.
Now it’s time to sort out the elevator itself, similar to what we did when mapping out the points for the moving platform. Go ahead and use the elevator object to create two points, an Origin point and a Target point with all their components removed so that the player can’t see it.
You’ll want the origin point set to where the elevator starts outside the player’s reach and the target point set to where you want the player to board the elevator.
We’ll also want to make sure that there’s a collider that covers the inside of the elevator so that when the player passes into it they will be parented to the object which will prevent any jitter while it moves up and down. To do that, just add a second Box Collider to the elevator and adjust it. Don’t forget to set it as a trigger and add the Rigidbody too!
With the points mapped out and the collider inside the elevator it’s time to make the elevator move when we want it to, create an Elevator script and add in this code:
private bool _goingDown = false;[SerializeField]
private Transform _origin, _target;
private float _speed = 2.5f;public void CallElevator()
{
_goingDown = !_goingDown;
}private void FixedUpdate()
{
if (_goingDown == true)
{
transform.position = Vector3.MoveTowards(transform.position, _target.position, _speed * Time.deltaTime);
}
else if (_goingDown == false)
{
transform.position = Vector3.MoveTowards(transform.position, _origin.position, _speed * Time.deltaTime);
}
}private void OnTriggerEnter(Collider other)
{
if (other.tag == "Player")
{
other.transform.parent = this.transform;
}
}private void OnTriggerExit(Collider other)
{
if (other.tag == "Player")
{
other.transform.parent = null;
}
}
Nothing too fancy here since it mostly code from our moving platform script but one cool thing you’ll notice is _goingDown = !_goingDown. This little bit of code here means that _goingDown is going to be assigned the opposite value of _goingDown. So in the first instance _goingDown is set to false, when the CallElevator() method is called it will set it to true which our FixedUpdate will pick up on and move the elevator down towards the target position.
Then when CallElevator() is run again it will set _goingDown to false and the opposite will happen. Speaking of that, let’s go ahead and tweak the ElevatorPanel script so that it can call this method when the player presses E:
private Elevator _elevator;private void Start()
{
_elevator = GameObject.Find("Elevator").GetComponent<Elevator>();
if (_elevator == null)
{
Debug.LogError("The elevator is null");
}
}private void OnTriggerStay(Collider other)
{
if (other.tag == "Player")
{
if (Input.GetKeyDown(KeyCode.E))
{
callButton.material.color = Color.green; _elevator.CallElevator();
}
}
}
In here we’re simply getting a reference to the Elevator as we normally would and then using that to use the CallElevator() method. I’ve also expanded the collider on the Elevator Panel object I had so that it stretches out into the elevator allowing for this:
Pretty good success I’d say, this is similar to what we did for the moving platforms except now we’ve given the player control over the movement.