Moving Platforms in Unity

Benjamin Calvin
3 min readMay 11, 2023

--

Today we are talking about how to implement a moving platform and how to have the player match the platform transform.

Create a platform of your choice. This is what I made:

Ensure there is a box collider and a rigidbody with gravity off. Check isTrigger on. Ensure the box collider is a little higher than the floor so there is a way to set off the trigger.

Turn this into a prefab, name it platform. Next thing we are going to do is make origin and target points. Easiest way is make two cubes and put them where you want. Name one origin and target or start and end. Whatever makes sense to you. Ensure the points are center of where the platform will be as that is what the platform is relying on.

After you find the right place, turn off the Mesh Renderer. Now you have points in world space.

Next make a C# script called MovingPlatform.

//set platform speed
private float _speed = 1.0f;
//assign origin and target transforms
[SerializeField] private Transform origin, target;
//create a seperate variable to hold the original target data
private Vector3 firstTarget;

private void Start()
{
//store the original target Vector3
firstTarget = target.position;

}


void Update()
{
//create a variable to handle speed for Vector3 MoveTowards method
var step = _speed * Time.deltaTime;
//if player current position is the same as target,
//make the target the origin
if (transform.position == target.position)
{
target.position = origin.position;
}
//swap it back to the original target position
if (transform.position == origin.position)
{
target.position = firstTarget;

}
//Use move towards method to whatever the target is.
transform.position = Vector3.MoveTowards(transform.position, target.position, step);


}
//when player triggers the collider, the platform turns the player
//turns the player into a child object of the moving platform
//making the player move the same way as the platform

private void OnTriggerEnter(Collider other)
{
if (other.tag == "Player")
{
other.transform.parent = this.transform;
}
}
//instead of being stuck as the child of the platform when jumping off
//and still making the same moves as the platform, turn the player back to
//it's own parent.
private void OnTriggerExit(Collider other)
{
if (other.tag == "Player")
{
other.transform.parent = null;
}
}

This will create the moving platform like what you would normally see in games. This will not be the last time I will be using this platform. It would be a good idea to create a parent containing the platform prefab and the points. Once this is organized this way, turn it into a prefab and call it Moving_Platform.

This way when you implement it somewhere else, the object can be placed in the Scene and adjust the points as desired

There are a couple side notes.

Troubleshooting Step 1:

When I land on the moving platform, the character jitters around.

This can be solved by using FixedUpdate() instead of Update()

Troubleshooting Step 2:

The player is being assigned to the platform, but the player isn’t moving with the platform.

Two Fixes. Unity turned off Auto Sync Transforms back in 2018 that caused this issue. Turning Auto Sync Transform on will fix that problem. This can be found in EDIT > PROJECT SETTINGS > PHYSICS > AUTO SYNC TRANSFORMS. Check the box and try again.

Another direction you could take is using FixedUpdate().

Thanks for reading, have a great day.

--

--

Benjamin Calvin

Dedicated and motivated individual learning programming and sharing my discoveries with you.