Creating a Moving Platform In Our Unity Platformer Game

A platformer wouldn’t be complete without moving platforms. Here I will demonstrate how to set up a simple moving platform for Unity games. When finished it will look like this:

First thing we need to do is to create game objects that will comprise the moving platform. Here there is a parent object, Moving Platform, that contains the 3 things we need to get this done. The 3 objects are the platform itself and the 2 points that the platform will move between:

The points are created by simply duplicating the moving platform and turning off or removing the colliders and mesh renderers:

After this is done, we have a platform and 2 “invisible” points that it moves between:

With the game objects set up, we can now create a script that actually moves the platform:

We basically keep track of whether the platform is moving right or left. If its moving right, we use Vector3.MoveTowards to move it to _point B, which is place at the right extreme of the platform’s movement. If it’s moving left, we move towards _point A, which is the far left point. When we get to either point, we switch movement directions.

Now that we have the platform moving, we need to set the player’s parent to the platform so that the player will move with the platform while they are on it. This can be done easily with the following code:

This last part involves On Trigger Enter and On Trigger Exit. Here we want to check when our player lands on the moving platform. When they enter the platform we set the player’s parent to the platform so that the player will move with the platform. When the player leaves the platform we set the player’s parent to null so that it will stop inheriting the platform’s movement.

Here you can see the results of the On Trigger Enter Method, the player’s parent is now the moving platform:

This allows the player to move with the platform:

That’s all there is to creating a moving platform in Unity.

A Unity Developer, interested in all things Unity.