My journey becoming a Unity game developer: 2.5D Infinite Runner-Moving Platform with player

Rhett Haynes
6 min readDec 19, 2021

--

Objective: Setup the Moving Platform to go from point A to point B. Also, have the player be able to stand and jump off the moving platform without falling through or off the platform.

Player can stand on the moving platform without falling off and jump off the platform as well.

First thing we’re going to do is have the Main Camera follow the Player as they move from one platform to another. Take the Main Camera and make it a child of the Player object.

Main Camera is a child of the Player object.

Next, we’re going to change the view of the Scene and Game View to be in a 2D format. Click on the 2D button in the Unity Toolbar to change the view in the Scene View. Then select the Main Camera object, and use CTRL->SHIFT->F to Align with View the Scene’s view to the Game View.

Game changed to 2D view and Aligned with View.

We need to add a Rigidbody to the Moving Platform for collision detection between the Player and the platform itself. Make sure to turn off the Use Gravity setting so the platform doesn’t fall downward instantly.

Rigidbody added to the Moving Platform.

Now create a new script name MovingPlatform, and inside the script let’s create new variables. The first one will be Public Type Transform name targetA and targetB as these variables will be place holders for the actual game objects the Moving Platform will move between. The next variable will have a Type Float name speed set to move the platform at 3 meters per second. Last variable will be a Type Bool name switching set to False initially which will determine if the Moving Platform has reached targetB or not.

Moving Platform variables assigned.

Since the Moving Platform has a Rigidbody attached to it, we will use the FixedUpdate() function for the platform’s movement. The FixedUpdate() function will provide a smoother, more accurate movement for the platform in the game.

We will use a number of IF conditions to check in this update. First, check IF the switching variable equals False. If it is, set the transform position to use the Vector 3 MoveTowards() method. The parameters we will set is the Moving Platform’s current transform position to move from, then the targetB position the platform will move to, and multiply the speed of the platform by Time.deltaTime to make the platform move in actual real-time.

IF switching equals True, then set the transform position to use the Vector 3 MoveTowards() function again. The parameters we will set is the Moving Platform’s current transform position to move from, then move to targetA position, and multiply the speed of the platform by Time.deltaTime.

To set the switching of directions of the Moving Platform, IF the platform’s position equals targetB position, we will set the switching variable to True. Else, IF the transform’s position equals targetA position, set switching to False.

Using Fixed Update to handle the platform’s movement.

Finally, we will use the OnTriggerEnter() function to check IF the Player collided using the Player’s Tag. If the Player collided, set the Moving Platform’s Transform to be the Parent object of the Player object.

After the Player lands on the Moving Platform, we need to use the OnTriggerExit() function to allow the Player to get off the platform. Check IF the Player is the active object still colliding with the Moving Platform using their Tag. If it’s the Player, set the Moving Platform’s Transform to not be the Parent of the Player making it equal to NULL.

OnTrigger() function used for when the player lands on and off the platform.

Back in the Hierarchy, place the Moving Platform script onto the Moving Platform game object. Then, drag the Point A and Point B game objects into their target slots under the Moving Platform component.

Moving Platform script assigned with the other platform’s positions.

When we play the game, the Moving Platform will actually fly away rotating offscreen.

Moving Platform moving, but rotating out of the screen.

With the Moving Platform selected, under Constraints we will Freeze Rotation of the X, Y, and Z-axis in the Rigidbody component.

Use the Constraints to Freeze Rotation on the X,Y, and Z-axis of the Moving Platform.

Play the game again, and see that the player will not stay on the Moving Platform no matter how much we try to stay on. This is happening because the Player isn’t colliding with the Moving Platform’s collider properly.

Player can’t stay on the moving platform.

One of the ways this can be fixed is to add another Box Collider to the Moving Platform. Turn on the Is Trigger setting, and move the collider UP along the Y-axis to around .35 just to make the collider be above the Moving Platform when the Player lands on it. With Is Trigger turned on, the Player can go into the 2nd collider and stay within the collider properly allowing the Player to become a child of the Moving Platform.

However, for the Player to be able to stand on the Moving Platform without falling off, select the Player object. Turn on the Is Trigger under the Capsule Collider so this trigger works with the 1st Box Collider attached to the Moving Platform which Is Trigger is turned off.

Is Trigger turned on for the Capsule collider of the player which allow them to stay on the platform.

Try the game once more and notice the Player can stand on the Moving Platform without sliding off or any glitchy movements with the player on the platform as well because of collider and rigidbody conflicts.

Player can stand on the platform without falling off.

We’re going to make the Moving Platform stand out from the other platforms by creating a new material and choose any color you like for the platform to look. Drag the material onto the Moving Platform for that platform to be the new color.

To organize the Moving Platform and the target game objects used, let’s modularize the Moving Platform by creating an Empty game object name MovingPlatform. Set its Transform positions to zero. Select the Moving Platform, targetA, and targetB objects and move them into the MovingPlatform game object. Set the Transform X-axis position to zero on all 3 objects, and drag the MovingPlatform object and its children into the Prefab folder.

Modularize the Moving Platform and its target objects within a new Moving Platform game object.

Just drag the Moving Platform into the Scene to create a 2nd moving platform for the player to navigate on. Position the targetA and targetB objects to a spot you want the player to jump onto the platform from.

2nd Moving Platform added to the scene.

Next time we will work on Losing a Life and Respawning.

--

--

Rhett Haynes
Rhett Haynes

Written by Rhett Haynes

Learning to become a Unity game developer.

No responses yet