My journey becoming a Unity game developer: 2.5D Infinite Runner- Player using the Elevator and Moving Platform

Rhett Haynes
8 min readDec 22, 2021

--

Objective: Setup the Player to call the elevator by pressing a specific key, get on the elevator, and get off on the next floor. Also, allow the player to stand, plus jump on and off the Moving Platform.

Player using the Moving Platform and Elevator.

Let’s begin with adjusting the Elevator Panel’s Box Collider to extend far enough on the X-axis to reach the elevator when it comes down. With the Box Collider Size on the X-axis, we want the collider to be just enough on the outside of the Elevator Panel so when the player enters the collider, they will be a little bit in front of the panel. The other end of the collider will extend out just a little past the back piece of the elevator. This will allow the player to press the “E” key again since the player is still inside of the panel’s collider to make the elevator go up to the next floor.

Elevator panel’s collider extended to reach the actual elevator itself.

Next thing we need is 2 targets for the Elevator to move between from one floor to another. Right-click on the Elevator Container, create a new cube and name it Origin. Select the Elevator game object, and Copy Component of the Transform. Select the Origin object and Paste Component Values from the Elevator onto the Origin object which will place the Origin object directly into the same position of the Elevator itself.

Duplicate the Origin object and name it Target. Drag the Target object down until it’s level with the lower level platform. With both the Origin and Target objects, either turn off or remove the Mesh Renderer, and remove the Box Colliders to prevent any weird collider collisions. We only need their Transform positions for the elevator to move to their preset positions when needed to.

Origin and Target objects created for the Elevator to move between.

Create a new C# script name Elevator. We need to create 4 variables for the Elevator starting with a Public or SerializeField Type Transform variables name origin and target. Next variable will be a Public or SerializeField Type Float name speed which will be set to 1f second per meter. Last, a variable Type Bool name goingDown which will be set to False.

Variables for the Elevator when moving.

Create a method Public Type Void name CallElevator() which will simply check to see if the Elevator is going up or down. If it’s going down, it will change the goingDown variable to True. If it’s not going down, it will change the variable to False.

When the Elevator is called, is it going down or not.

Then using FixedUpdate() method for handling the Rigidbody Physics objects and consistency, check IF goingDown equal True. If it does, set the transform position of the Elevator to equal a Type Vector 3 using the MoveTowards method to move the current transform position to the target position with speed multiplied by Time.deltaTime for real-time movement.

Else, we will check IF goingDown equals False. If it does, using Type Vector 3 MoveTowards again, we will get the current transform position to the origin of the Elevator’s transform position with speed multiplied by Time.deltaTime for real-time movement.

Using Fixed Update() and MoveTowards() functions, check IF the Elevator is going down or up.

Another way we can move the Elevator between targets is using the Lerp() method. Instead we will replace the MoveTowards() method after the Type Vector 3, and replace it with Lerp(). Lerp() will linearly interpolate or estimating an unknown value between the 2 given points which will be the origin and target objects. Set the 1st parameter to the current transform position. The 2nd parameter to the target position IF goingDown equals True or origin position IF goingDown equals False. The 3rd parameter will be the time it will take to move between the points which is the speed multiplied by Time.deltaTime. We will notice the elevator moving gradually from one point to the other just like a real elevator.

Using Fixed Update() and Lerp() functions, check IF the Elevator is going down or up.

We will use the OnTriggerEnter() and OnTriggerExit() to allow the player to enter the elevator and leave the elevator when needed. With both functions we will check IF the other object that collided with the collider in the Elevator is the Player using their Tag. You can use Tag or CompareTag for this situation. If the Player did enter the elevator, make the Elevator the Parent of the Player. If the Player exits the Elevator, make the Player not a Child of the Elevator.

Using OnTriggerEnter(), check IF the Player is entering or exiting the elevator.

Create another script name ElevatorPanel which will check if the Player has pressed the button to call the elevator. Create 3 new variables starting with a Public or SerializeField Type MeshRenderer name callButton. The next variable will be a handle to the Elevator component with a Type Elevator name elevator. The last variable will be a Type Bool name elevatorCalled set to False.

Variables to check on the panel’s button and the Elevator itself.

Inside the Start() function, we will use the elevator variable to get the Elevator script component by finding the GameObject.

Gain access to the Elevator object, and checking IF the elevator exists.

While standing inside of the Elevator Panel’s collider, we want to use the OnTriggerStay() function to check IF the Tag of the object inside of the collider is the Player. If it is, we want to check IF the “E” key was pressed. If so, then we want to check IF the elevatorCalled is set to True. If it’s True, we’re going to change the callButton material color to Red.

Else, we’re going to change the callButton material color to Green, and use the elevator variable to Call the Elevator which will set goingDown to True.

When the Player is inside the Panel’s collider, checking IF the Player pressed the button to call the elevator.

Over at the Moving Platform, we want to make sure it has a Rigidbody for the player to collide with this object. Turn off Use Gravity and Freeze the Rotation of the X, Y, and Z-axis to keep the platform from doing anything weird when the Player jumps on it.

One important thing we must do is give this platform 2 Box Colliders as the Player will not stay on the platform properly when they are on it. The 1st collider will be the around the platform as it originally was with the Is Trigger turned off. The 2nd collider will be move moved upward enough to allow the Player to stand inside of the collider. Also, we will turn on the Is Trigger as this will allow the Player to enter the collider, but more importantly stand on the platform while they are in this collider.

Moving platform setup.

To set up our target for the Moving Platform, we can use either 2 Cubes or Empty objects. Name each one Point A and Point B with point A being the origin position, and point B as the target position. With the Cubes, make sure to remove the Mesh Renderer and Box Colliders.

To set their positions, select the Moving Platform and Copy Component values of the platform. Then, select both Point A and Point B and Paste Component Values to them and this will place them directly at the Moving Platform’s position. Select Point B and drag up to the desired location for the platform to stop at before coming back down.

If you’re using an Empty object and having a little trouble seeing the exact placement of the targets, select both target objects and click on the Icon setting next to the name of the target objects. Select a color you like, then turn on Gizmos and slide the Gizmo bar to make the Icon appear bigger onscreen to your liking.

Setup the Moving Platform to move from Point A to Point B.

See the player jumping on the Moving Platform and not falling through it or off. Also, notice the platform moving to the Icon gizmos being used to gauge the placement of the target object’s positions.

Platform moving from Point A to Point B with the player on it.

Now with the Elevator, we see the player enter the Elevator Panel collider’s zone and when we press the “E” key, the elevator comes down. After the player gets on, we press the “E” key again and move up to the next level. Also, we can jump on and off the Moving Platform. However, notice the Player on the Elevator as the elevator is jittering while it moves up to the next level.

Player using the elevator and moving platform with the MoveTowards() function.

The reason it’s jittering is that the Player is standing on the Elevator’s collider, but not inside of it. Therefore, their is a conflict happening between the Player and Elevator’s collider. To fix this just select the Elevator and add another Box Collider. Adjust its height and make sure it’s tall enough for the player to stand inside of it, and have the bottom of the collider a little bit below the Elevator’s floor. Turn on Is Trigger so the Player can stand inside of the collider, and not have to interact with the other collider attached to the Elevator’s floor.

Elevator given a 2nd collider with Is Trigger turned on and positioned inside the elevator.

Player using the MoveTowards() function.

Player using the Elevator with the Lerp() function.

Player using the elevator and moving platform with the Lerp() function.

The MoveTowards() and Lerp() gives similiar, but different results. With the MoveTowards() function, we see the elevator moving at a steady speed. While, the Lerp() function the elevator moves quickly then slows down as the it arrives at its destination. Both functions sometimes gives frustrating results when pressing the key to call the elevator to move. However, both works and it comes down to preference.

Next time we will work on Collecting Coins and Wall Jumping.

--

--

Rhett Haynes
Rhett Haynes

Written by Rhett Haynes

Learning to become a Unity game developer.

No responses yet