Creating Moving Platforms in Unity

Andrea Zilio
3 min readJul 18, 2021

We have implemented basic features such as a character controller to move and jump as well as some collectables. While still working in the prototype setup let’s add some movement to the platforms.

A Camera that follows the player

To make the game playable we need the camera to follow the player. It is done by simply attaching the main camera to the player.

Decide which platform and create reference points

We then choose a platform and create two copies. The copies will serve as reference points. We can rename them PointA and PointB and delete all the components other than the transform. All of these we can store in an empty game object called Moving_Platform_Holder.

I have also created green materials to make the moving platform easily distinguishable.

The Moving Platform Script

We create a new moving Plattform script and attach it to the moving platform. pWe need to first assign the two reference points.

There are a number of solutions to create a moving platform. We are going to use the Vector3.MoveTowards class. To decide towards which point we move, we simply add abool switchDir variable. This is how the script looks:

It works. As you will see, however, the player does not yet move with the platform. We can fix this by detecting the collision.

As the update method depends on your computer’s performance, we get a smoother “attachment” if we use FixedUpdate method. As the API documentation explains, “MonoBehaviour.FixedUpdate has the frequency of the physics system; it is called every fixed frame-rate frame.

I would also recommend you add an additional box collider or change the existing one to include more space above the platform. This will improve the collision detection.

Modularized moving platform

As a last step we can modularize the moving platform. To do so we set the x position back to 0 on the platform, point A and B. Then we create a prefab. We can now easily create additional moving platforms and just set the new positions.

--

--

Andrea Zilio

Passionate Game Developer and Learning Expert. I love to create games and interactive experiences using Unity, Articulate, C#, JavaScript, PHP, HTML, CSS.