Move your player with your platforms in Unity
Objective: Move the player along with a moving platform in Unity
My previous article was all about moving the platforms between two points. Now it is making sure that when the player jumps on the platform that they move with the platform.
I will achieve this by changing the parent of the player to the platform and then change it back to null.
First, I added in another box collider to the platform and made it a trigger. I then made sure that it is sticking out a little bit so that I know that the player will always be in the trigger is they are on the platform.
Now comes the code:
I am using the two trigger methods enter and exit.
When the the player enters the collider I make sure that the object is the player, then I change the parent of the player to that of the platform. This moves the player with the platform. If you do not do this, you will find that the player will stay still on the platform and will fall off.
To make sure that once the player is off the platform that the are no longer tethered to the platform I am using the OnTriggerExit method. This will run every time that the player exits the trigger volume. In this method I am changing the parent back to null.
And that’s it for moving the player with the platform.