Moving Platforms in Unity
Create a moving platform from A to B!
The idea is to have a moving platform go back and forth from point A to B. So in this case, the transform of the object will have to be manipulated whether if it's going horizontally from the ‘x’ position, vertically from the ‘y’ position, or forward from the ‘z’ position.
Creating the Platform
The first thing I did was to create the platform game object using Pro Builder.
I duplicated the game object twice so that I can capture its transform positions. I renamed the first to “PointA” and the second to “PointB”. I then moved “PointB” to the desired position where I want the platform to move to.
Since I only need its position, I deleted all of its components and only contained the Transform component on both “PointA” and “PointB.”
Creating the Script
Now that I have the platform game object all set with point A and B positions set, I can go ahead and create the script.
I created a new C# script and named it “MovingPlatform.” I attached it to the “MovingPlatform” game object.
The script is going to tell the platform:
- Hey, go to point B.
- And if at point B, go to point A.
- And if at point A, go to point B.
I want it to go back and forth.
For this, I am going to use:
Vector3.MoveTowards(current position, target position, speed)
It calculates a position between the points specified by current (PointA) and target (PointB) position.
With this information, I know that we need to define a current position, a target position, and a float variable for speed.
[SerializeField] private Transform pointA, pointB;
[SerializeField] private float speed = 1f;
private bool _switching = false;
I like to keep my members private, and when I need to expose them in the inspector, I Serialize Field them.
You may notice an extra member of type bool. This will be used to create the logic to switch between positions.
In FixedUpdate, if
_switching is false, move the current position of the moving platform game object to the target position of point B.
FixedUpdate() you may ask? Fixed update is a physics-based update with a consistent time stamp. If we use
Update(), the player game object will jitter on the platform.
_switching is true, then we need to move the current position of the moving platform game object to the target position of point A.
transform.position = Vector3.MoveTowards(transform.position, pointB.position, speed * Time.deltaTime);
transform.position = Vector3.MoveTowards(transform.position, pointA.position, speed * Time.deltaTime);
After this is set, I create the logic to check if the current position equals a target position.
if(transform.position == pointB.position)
switching = true;
else if(transform.position == pointA.position)
switching = false;
With this in place, my platform can now go back and forth!
Player moving with the Platform
What happens when we want to move the platform forward or horizontally? Yep, the player will fall and not move with the platform! So I have to figure out a way to parent the player to the moving platform.
I added a Box Collider to the moving platform game object and marked it as a trigger. I also centered the ‘y’ position to 1. It will allow the player collide to with the trigger.
With this set, we can have the MovingPlatform script collision check to see if the player has collided with the trigger. And if so, we can assign the transform parent of the player to the moving platform transform.
private void OnTriggerEnter(Collider other)
other.transform.parent = this.transform;
I also want the player to jump out of the moving platform and not stay parented to it.
private void OnTriggerExit(Collider other)
other.transform.parent = null;
If the player jumps out, I want to make sure the player’s transform parent is null.
Modularize the Platform
Now I can modularize the moving platform content so that I can turn it into a prefab and use it several times in other locations and scenes!
This is quite rudimentary for a moving platform but it works. There are many ways to go about this too! And if you have another solution, please share your thoughts in the comments section!
Thank you for your time!