2.5D Training Progression Report: Elevator & Pushing Systems. Unity

Following up from the last progression report, I have implemented two new systems. An elevator system where the player pushes a call button to call the elevator and then the elevator goes back to its original position; and a pushing system where the player pushes a block for example to activate a switch.

Elevator System

The idea here is to allow the player to call the elevator only when he has collected enough coins(amount of collected coins is specific to each call button), and once the button is pressed, the elevator gets called down.

The elevator button needs to know about the elevator and the amount of coins collected by player in order to allow the call. This is done in the OnTriggerStay method which will be used to call the elevator down and up(we extend the collider to reach the elevator position).

private void OnTriggerStay(Collider other)
{
if (other.CompareTag("Player"))
{
Player _player = other.GetComponentInParent<Player>();
if (_player != null)
{
if (Input.GetKeyDown(KeyCode.E) && _player.CoinsCollected() >= _requiredCoins)
{
_panelLight.material.color = _panelLight.material.color == Color.green ? Color.red : Color.green;
_elevator.CallElevator();
}
}
}
}

In the elevator script, we allow it to using Vector3.MoveTowards. The waypoints are predefined and use a Boolean to change the waypoints based on if the elevator is called to call button or not. In other words, if the player is calling it down then waypoint is call button, if the player is on the elevator then waypoint is above. (In this project, player cannot go back down).

private void FixedUpdate()
{
var step = _speed * Time.deltaTime;
transform.position = Vector3.MoveTowards(transform.position,
_isCalledToPanel ? _pointB.position : _pointA.position, step);
}
public void CallElevator()
{
_isCalledToPanel = !_isCalledToPanel;
}

Pushing System

In this system, the player must push a block to a key location / switch in order to finish the level or maybe activate a door maybe.

In order to achieve this, we took advantage of the OnControllerColliderHit method that comes with the Character Controller. We detect the movable_block by tag if it collided with the player and then get hold of the rigidbody of this block. We check if it is not kinematic and not below us and then upon player input detect the move direction of the registered hit and then push the rigidbody of the cube thru its velocity.

private void OnControllerColliderHit(ControllerColliderHit hit)
{
if(hit != null)
{
if (hit.collider.CompareTag("Movable Object"))
{
var _movableObject = hit.collider.attachedRigidbody;
if (_movableObject.isKinematic == true)
return;
if (hit.moveDirection.y < -0.3f)
return;
if (Input.GetKey(KeyCode.E))
{
var pushDir = new Vector3(hit.moveDirection.x, 0);
_movableObject.velocity = pushDir * _pushPower;
}
}
}
}

Now the cube is moving, and on the pressure pad (switch) we create a script and use the OnTriggerStay method to detect the cube and check when to stop it and activate the switch.

Next up, using this prototype to create a 2.5D platformer and make use of URP in unity.

A knowledge seeking biologist who is following his passion into a full time career in Unity / Game Development. https://www.linkedin.com/in/mohamed-hijazi/