Platformer: Setting Player to Moving Platform

Ryan McCoach
3 min readSep 1, 2021

Introduction

In this article, we will cover when the Player lands on a moving platform the Player will move with the platform. When the Player jumps off of the moving platformer, they are no longer moving with the platformer.

Detecting Collision

On the moving platform, we are going another Box Collider so we can detect when the player jumps on the platform and jumps off. This Box Collider’s center will be moved up, so the Player can actually enter into the Box Collider.

In order for the Collider to detect if something has enter or exited, Is Trigger will need to be turned on.

In the MovingPlatform script, we are going to use the OnTriggerEnter for when the Player jumps onto the platform. We will use OnTriggerExit for when the Player jumps off the platform.

Parenting & Unparenting Player to the Platform

In the OnTriggerEnter, we will want to check if the other Collider has the Player tag then we will set the parent transform to the moving platform transform. This will make the moving platform the parent of the Player.

When we test it out we can see the Player is still not being parented.

This is the result of the moving platform being in the Update. We want to put the movement code in a FixedUpdate and this will fix the problem.

Quick Difference between Update & FixedUpdate courteous of John Tucker (link below).

https://john-tucker.medium.com/unity-update-versus-fixedupdate-7a4d4d7bc1f5

Lastly, when the Player jumps off the platform or exits the platform Collider we will set the parent transform of the Player to null. This makes it no longer a child of the moving platform.

There you have it! Now the Player moves with the moving platform.

--

--