Moving Platforms — A Side-Scroller Staple

Jack Leavey
2 min readOct 24, 2022

--

Moving platforms will add some challenge!

With the player, coins, and basic platforms created, it’s time to transition into some more challenging (albeit entry) gameplay!

Setting up a modular moving platform prefab is quick an easy, and allows designers to create challenging, unique networks for the player to navigate.

I created new C# script (MovingPlatform.cs) and set a few variables.

The array waypoints allows us to set the two locations the platform should move between for each individual system.

The float speed is self explanatory; it allows us to the speed of each platform uniquely.

Finally, the bool _switching is the indicator for the platform to know when to transition back to the first point, allowing for an infinite loop.

NOTE: It is important to utilize fixed update for these platforms, as they will interact with the physics of the player.

In FixedUpdate, we check our bool (which defaults to false) and then move towards the second waypoint. If our bool is true, we instead transition to the first waypoint, which is located at our starting position.

The following if/else statement handles when we flip the boolean; which is when we reach the transform.position of the waypoint we are currently moving towards.

NOTE: The platform will work with just these lines, but the player will not move in tandem with the platform in the current state, leading to some undesirable gameplay.

To fix this issue, we need to parent (and unparent) the player to the platform using an OnTriggerEnter and OnTriggerExit method:

OnTriggerEnter
OnTriggerExit

When the player is parented to the platform, the player will be moved at the same speed and direction, as the player is considered a child to the platform during that time. When the player leaps off the platform, this will unparent the player, giving the player full control back!

--

--

Jack Leavey
Jack Leavey

Written by Jack Leavey

I am a software engineer with years of experience branching into game development, specifically in Unity. Follow along for guides on creating game mechanics!

No responses yet