Unity Development — Moving Platforms

Players jumping from platform to platform is a standard piece of gaming level design. It is only natural to add a bit of challenge by making those platforms move.

Make a simple platform with a Cube. You can add a new material to it if you wish to change the color and have it stand out from the other platforms. Now create two empty GameObjects, and place them in the two positions you want the platform to go between.

Make a MovingPlatform script and lets get into the code. First, you will need a reference to the empty GameObjects. You can refer to them as ‘targets’ or ‘points.’ Serialize them so you can simply drag them in later.

To make the platform move, we are going to use a Vector3 function called ‘MoveTowards.’ You simply set the current position, the target position, and the speed. Since we want the platform to go back and forth and not stop at the end, we are going to need to switch the direction and the ‘MoveTowards’ call. For that we will need a boolean.

With this bool, we can establish which direction the platform is going. If it is going forward and gets to the target, we tell it to go backward. If going backward and gets to the target, tell it to go forward. Do all of this in a FixedUpdate() and it will work out great. Here’s what the code will look like.

You can set the speed on the end of the MoveTowards function to whatever you like.

Try out different paths and see how this helps out your level design skills.