How to create a moving platform in Unity

Luke Duckett
4 min readJul 19, 2021

In this quick article I will go over how to create a moving platform in Unity.

Our moving platform will move between set targets continuously, we will create a visual representation of the targets as children when we create the platform. We will then write a script which unparents these targets from out platform(otherwise they will continue to move with the parent and it will never reach its destination). We will then add the movement functionality to our script.

The first step is to create the platform. Create a 3d cube and call it “Moving_Platform”. Stretch it out a little bit so it looks like a platform then add a child object called Target_A (we will add more in a moment).

On Target_A remove change the icon so its easier to see (if you can’t see gizmos in your scene go to “Scene” > “Gizmos” > “3D Icons” and turn them on.

Generally, I would leave Target_A at the starting position. Next duplicate your Target_A object (ctrl + d) and rename to “Target_B”. Move this target to the location you want your moving target to move to.

You can continue this process as you’d like until you have all the targets you wish. I’m going to create one more above my second target. I’m going to create an empty game object as a container for all our target objects (this is just to keep the hierarchy clean).

Next step is to create our script. We are going need a list of our targets, a bool to see if the platform is looping back, an int to determine our current target, a game object reference to our target container and finally a float for our speed.

First step is to get a reference to our target container and null check.

Next create a function that will set up our target list. This function will be called in start. The function will add all our child objects to our target list then it will change the parent of those objects to the Target container.

The following step is to create a script that moves our platform to our current target and checks if we’ve arrived. We do this using “Vector3.MoveTowards“ on our position, passing in the current target int into the targets list. We check the distance and if we are close we can then increment our target which we will do by another function “IncrementTarget”. We will run this in fixed update so the platform is able to move the player without getting out of sync.

In this method we check to see if we are looping. When we are looping we are going back through our targets and therefore need to decrement our current target. If we are not looping we then increment the target. In order to know if we have reached the first or last target we check to see if the targets are either more or less than our targets list, then we can see looping to true or false. Be careful of getting an out of bounds error. You could get this if your current target is less than 0 or more then your target count -1 (as your list is 0 indexed i.e. starts from 0).

Next we can prefab our game object and add our script. As we have not added a default speed feel free to do this now too.

Now this is done, our next step is to check for when the player lands on the platform. We do this check to ensure the player moves with the platform (by childing it to the platform). We add an on trigger enter and on trigger exit event to the platform and check for the player tag. If the object that collided was the player we can then adjust its parenting appropriately.

To ensure this works ensure the player is tagged as player and then we need to add a rigid body (no gravity) and another trigger collider to the platform with a slight offset on the y value so the collision can be triggered reliably.

Now we can test the final results.

That’s all for now.

--

--

Luke Duckett

🎮 First Nations Unity Dev from Wonnarua country 🏞️ | From Player to Lifelong Learner: Crafting Games, Debugging Code, and Embracing New Technology