Nerd For Tech
Published in

Nerd For Tech

Moving Platform

Objective: script a moving platform with modular path

Another evergreen platform component is the moving platform. We are going to create and script it in a modular way, so we can easily duplicate its behaviour and just change the waypoints defining the path.

Yes, waypoints. In fact I’m going to reuse/rethink a large part of the code I wrote for the patrolling guards (here and here) in my previous game The Great Fleece.

Let’s design something first:

  1. The player must be well anchored to the platform, unless it jumps off, meaning that it will move with the platform

And that’s it.

Let’s start from the second point, which is way simpler. To make the player stick with the platform, the easiest way is to make it a child of the platform the moment the player lands on it and take it out when jumping.

In order to achieve this kind of behaviour, we need to add another box collider component to the platform, a trigger, leaning out of the platform in order to detect the landing. Of course we need a rigid body, with no gravity.

To make the collider lean out we can just change the y component of the centre. We also need to freeze x-y-z rotations to avoid unwanted strange colliding behaviours with the character.

Now the script, MovingPlatform.

We set the player child to the platform, or not, on trigger enter, or exit.

Now let’s look at the movement itself. We are going to perform it in the FixedUpdate() method that ensures a consistent timestamp, resulting in a smoother effect.

WaypointsGood() method returns a bool saying if there are waypoints in the list and it is safe to access them. Then at line 5 we check if the current waypoint has been reached.

If yes, we evaluate the next waypoint (7)and if the platform is in the first or last waypoint we start a coroutine to make it wait, otherwise we make it wait for a shorter time, if the wait-on-waypoint option is enabled.

If the waypoint has not been reached yet, the platform moves, 16.

The next waypoint is evaluated by changing the id, or list index. But if the limits are reached the direction of the path changes so from 0 to max the index is incremented, then starting from max is decremented and so on.

The platform is stopped with this smart trick. Since we have no possibility to wait and stop the movement from inside the FixedUpdate() we don’t actually stop it! We just put the speed to zero with a coroutine! Inside the coroutine we can wait and then restore the speed.

The actual motion is performed via the MoveTowards() method of Vector3 class.

Once the script is finished, we just need to properly populate it from the inspector.

Being the waypoints just transform components of empty objects.

Now, we can play and get fun by adding, removing or replacing the waypoints and have completely new paths: horizontal, vertical or diagonal!

Let’s look at the results.



NFT is an Educational Media House. Our mission is to bring the invaluable knowledge and experiences of experts from all over the world to the novice. To know more about us, visit

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Daniele Quero, PhD

A professional developer with passion for game developing and skill-growing. A former Nuclear Physics Researcher who changed his life to pursue his dreams