Day 95 — Improving the Elevator

Hey and welcome!

Next up is the code itself in the Elevator script, it’s going to take quite a drastic change! Which is something I’ll have to leave for the next article as there’s a bit to talk about.

private bool _goingDown = false;
[SerializeField]
private Transform _origin, _midPoint, _target;
private float _speed = 2.5f;
private bool _elevatorCalled = false;
[SerializeField]
private float _middleDist;
public void ChangeDirection()
{
_goingDown = !_goingDown;
}
private void FixedUpdate()
{
_middleDist = Vector3.Distance(_midPoint.position, transform.position);
if (_goingDown == true && _elevatorCalled == true)
{
transform.position = Vector3.MoveTowards(transform.position, _target.position, _speed * Time.deltaTime);
if (_middleDist < 0.1f && _middleDist > -0.1f)
{
StartCoroutine(PauseTheElevator());
}
if (transform.position == _target.position)
{
StartCoroutine(PauseTheElevator());
ChangeDirection();
}
}
else if (_goingDown == false && _elevatorCalled == true)
{
transform.position = Vector3.MoveTowards(transform.position, _origin.position, _speed * Time.deltaTime);
if (_middleDist < 0.1f && _middleDist > -0.1f)
{
StartCoroutine(PauseTheElevator());
}
if (transform.position == _origin.position)
{
StartCoroutine(PauseTheElevator());
ChangeDirection();
}
}
}
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;
}
}
private void OnTriggerStay(Collider other)
{
if (other.tag == "Player")
{
if (Input.GetKeyDown(KeyCode.E) && _elevatorCalled == false)
{
_elevatorCalled = true;
ChangeDirection();
}
}
}
IEnumerator PauseTheElevator()
{
_speed = 0f;
yield return new WaitForSeconds(5.0f);
_speed = 2.5f;
}

There’s quite a bit going on here, our OnTriggerStay() event has had quite a big change to it as we’re now making sure that the elevator is only called once since it’s going to be moving up and down indefinitely now. Due to this I’ve changed the name of the CallElevator() method to ChangeDirection() now which gets called when the position of the elevator matches the origin point or the target point.

In order for the elevator to recognise when it’s close to the midpoint I’ve created a float variable called _middleDist that works out the distance between the elevator and the middle point. When this value is less than 0.1f we run a coroutine called PauseTheElevator() that sets the speed of the elevator to 0 and then pauses the code for 5 seconds before setting the speed back to 2.5f. This gives the illusion that the elevator stops on the middle floor before heading to the bottom or top floor.

I’ve also made sure that our code handling the MoveTowards only runs when the elevator has been called by the player. This stops it from moving off on its own when the game starts.

I think that covers everything there, you should have something like this working for you now:

There’s one more thing I want to do here though before I move onto newer horizons and that’s to add something to the UI letting the player know that they can in fact press the “E” key on the elevator to get things moving!

Start off by creating a new text object called along the lines of Elevator_Text and then style your text to your liking letting the player know to press E to start the elevator.

Now we need to head over to the UIManager script and add in two methods:

[Serialize]
private GameObject _elevatorText;
public void ActivateElevatorText()
{
elevatorText.SetActive(true);
}
public void DeactivateElevatorText()
{
elevatorText.SetActive(false);
}

These simply change the active status of the elevator text gameobject to true or false depending which is called, this is what affects the visibility of the text to the player.

To make use of that we’ll need to add and edit some code in the Elevator script:

private UIManager _uiManager;private void Start()
{
_uiManager = GameObject.Find("UIManager").GetComponent<UIManager>();
if (_uiManager == null)
{
Debug.LogError("The UIManager is null");
}
}
private void OnTriggerStay(Collider other)
{
if (other.tag == "Player")
{
if (_elevatorCalled == false)
{
_uiManager.ActivateElevatorText();
if (Input.GetKeyDown(KeyCode.E))
{
_uiManager.DeactivateElevatorText();
_elevatorCalled = true;
ChangeDirection();
}
}
}
}

You’ll notice here that the OnTriggerStay has been changed once more, this is to accomodate for our code to activate and deactivate the text gameobject so that the text stops being visible after the player has pressed the “E” key.

With that done we’re left with a pretty good elevator system in the game that lets the player know they can interact with it!

Hey and welcome! My name is Connor and my goal here is to put out a daily post for a full year about my game development journey.