Moving Platforms in Unity

We talk about a platform game, we already knew that sooner or later we would have moving platforms …

In fact, we have to make one that can be used all the time, which moves horizontally, vertically, and diagonal.

Let’s start from the cube, we take a cube, we modify its scale and we get the shape of a platform, now we have to connect to this cube a Rigidbody and deactivate the gravity.

Now we must define what will be its starting point and its ending point. To do this we simply duplicate our platform twice, move it to the positions we want, and remove all components except the transform, so that we end up with an empty game object. We rename the two points to point A and point B and move on to the code.

We will use a method of the class Vector3 “MoveTowards” that requires the current position of an object, its destination, and the step that will be our speed for the time of the game.
And we have to alternate when it will go towards point A and when it will go towards point B.

All that remains is to assign the script to our platform, insert point A and point B into the variables we created for them in the script and test it.
To prefab and start mass production of moving platforms, create a new game object and insert the three elements into it, then transcend it into the Prefabs folder to complete the operation.

Now all you have to do is put your prefab in the scene, move point a and point b, set the speed and you will have created any kind of moving platform you need.