Day 79 — Creating a Moving Platform in Unity

Hey and welcome!

Now here’s an interesting one that sounds easy enough to implement but surprisingly needs a bit of thought put into it.

Firstly though we’re going to need to make it so that our camera follows our player along the game view so that we can see our level. This part is pretty easy to do as you just need to drag and drop the Main Camera into the Player object in the hierarchy.

Pretty cool stuff and that was pretty easy to implement!

Next up we need to mark out our points for the moving platform, first find or add a platform that you want to move back and forth horizontally and rename it to be Moving_Platform or similar.

Now copy that object and call it PointA and then copy it again and move it along your x axis to where you want the end point for your moving platform to be and rename it PointB. These two points mark the position that our platform will move between so go ahead and remove the components from them so that the player can’t see or collide with these objects.

Let’s get the movement for the platform sorted out now, go ahead and create a MovingPlatform script and then add in this code:

public Transform pointA, pointB;
private float _speed = 2.0f;
private bool _reverse = false;
void Update()
{
if (_reverse == false)
{
transform.position = Vector3.MoveTowards(transform.position, pointB.position, _speed * Time.deltaTime);
}
else
{
transform.position = Vector3.MoveTowards(transform.position, pointA.position, _speed * Time.deltaTime);
}
if (transform.position == pointB.position)
{
_reverse = true;
}
else if (transform.position == pointA.position)
{
_reverse = false;
}
}

This is all code we’ve seen before, we’re using MoveTowards() to move the platform to the Point A and Point B positions that we determined before. In order for the platform to know which one to head towards we use the bool _reverse in order to let the platform reverse its movement back to Point A when it’s true and then head to Point B when it’s false.

If we tried doing this without the bool and attempted to move the platform in reverse when it matched the position of Point B then it would do it briefly and stop since the moment we move it the condition would no longer be true since it no longer matched the Point B position.

With that done though you should have something like this so far:

Now comes the tricky part which is getting our player to move along with the platform when they collide with it. Speaking of colliding, go ahead and add a second Box Collider component onto the moving platform object and then raise the y position for its center up a bit so that some of the collider is above the object itself.

Set this to a trigger collider as well. Since this is a collision we’re doing, make sure to add a Rigidbody component and turn the gravity off for it. We also need to do something new here, open up the constraints option and then check each box for the Freeze Rotation option.

This stops our player and moving platform objects from freaking out if they collide at a weird angle.

Now for the last part to do, add in the following code to your MovingPlatform script:

private void OnTriggerEnter(Collider other)
{
if (other.tag == "Player")
{
other.transform.parent = this.transform;
}
}
private void OnTriggerExit(Collider other)
{
if (other.tag == "Player")
{
other.transform.parent = null;
}
}

This parents the player object to the moving platform object allowing the player to move along with it much like the camera does since it’s parented to the player except this time we’re doing it through code.

We also have an exit trigger here so that our player object un-parents itself when it jumps off the platform. If we didn’t do that then you would find that the player moves off by themselves even when they’re not on the platform!

That’s everything on the topic of moving platforms, remember that since we’re using MoveTowards these platforms can move vertically or even diagonally depending where you place the PointA and PointB so it’s worth creating and empty object to stick the Moving Platform and Point objects inside of so that you can prefab it and use these later.