Moving Platforms in Unity

Simon Leen
3 min readAug 20, 2021

Objective: Implement a moving platform in Unity

To start off with this we will set the camera to follow the Player. We can easily do this by making the camera a child object of the Player. To make the moving platform more visable in the inspector we can rename that specific platform to moving platform.

Create a script called MovingPlatform and attach it to the moving platform object. We then create 2 empty objects and call them point a and b. We set the postion of the platform start point to a and the position we want the platform to move to point b.

To make the movement between positions possible, we create 2 transform objects called targetA and targetB, each assigned to its relative point. We need a speed variable of type float for how fast the platform moves. To get to platform moving between points we set a private bool called switching.

If switching is true the platform moves towards point b and vise versa.

Finally we need the Player to move with the platform when its moving. We start this process by adding a new box collider to the moving platform and set the center slightly above the platform so it can be used as a trigger.

We then set up an OnTriggerEnter on the moving player checking if the other tag is the Player and if so, setting the Players transform.parent to the moving platform making the Player a child of the moving platform which will move the Player with the platform. We now have an issue where the Player is jittery on the platform.

To fix this we change the Update method to a FixedUpdate which uses the Physics Update which is a consistent timestamp and removes the jitter. Now that the Player is moving smoothly with the platform we need to remove the Player as a child when the Player leaves the platform. For this we use the OnTriggerExit and set the Players transform.parent to null. A material was added to the moving platform to make it easier to identify in the scene.

To modularize the platform we create an empty object called Moving_Platform and place the platform and points inside it and zero all the positions. We then prefab the full object and now when we need a vertical or horizontal platform or even a diagonally moving platform we only need to set the position of point b.

In the next post we will look at Player lives.

--

--