Unity/C# Challenge 47: Player Moving with Platform

Challenge: Make the player “stick” to the moving platform.

Currently, we have a moving platform. However, when our player lands on it, they slide off:

To fix this, we need the player to parent to the moving platform when it is on it, and unparent when it is not. To do this, first we add a new box collider to our moving platform, which we will make a trigger and extend the collider out above the platform. Then we will change our Update method in our moving platform controller to FixedUpdate (which will smooth out our animations). Finally, we will update our script with:

Here, when our player enters the collider that we set up, it will set it’s parent as the moving platform. When it leaves, it will no longer be attached to a parent. While attached, the player will “stick” to the platform.