Create a Moving, Walkable Platform in Unity

Mike Brisson
3 min readJun 17, 2021

--

Whether it’s 2D, 3D, or even AR, moving platforms are a staple in game development. Moving a platform between 2 positions can be done many ways. Let’s start with one of the simplest.

First we need a platform to move. Create a cube and scale it horizontally:

Next, we need to locations; A start and end. We can add 2 empty game objects to the scene and use colored icons to show their positions:

With our start and end positions in the scene, we can now position them where we want them:

We can now create a script called “Platform” where we will implement Vector3.MoveTowards() in order to move between the start and end positions.

Let’s get access to the positions and create a bool that will tell us if we should be moving towards the beginning or end…we can add a speed variable while we are there too:

Next we’ll create a method that returns the next location as soon as we have reached our current target. We can do that with a simple ternary condition:

If _movingRight is true, move to _end…else move to _start

Now we can use the Vector3.MoveTowards() and add our new method as the target location. Then we just need to set our bool if we have hit either of our lcoations:

Your instinct might be to throw this Move() method in the Update() method…which would move the platform the way we want.
If we are using the character controller for our player, this WILL NOT WORK.

In order to avoid the headache (and save some precious frame calls as well), we can put our Move() method in the FixedUpdate() method which will give us the best of both worlds:

Last thing we need to do is to get our player moving with the platform. We need to do 2 things:
• add a trigger collider to our platform
• set the parent of our player to the platform while they are on it

The first part we can simple add a box collider, make sure it is set to “Is Trigger” then raise it slightly so that it overlaps our player:

Finally, we just need to edit the script to check for the player during OnTriggerStay() and OnTriggerExit():

--

--

No responses yet