Geek Culture
Published in

Geek Culture

2.5D Platformer: Moving Platforms, Part 1

In this article, I’ll create movable platforms that can provide an extra challenge to the Player.

I’ll first rename one of the platforms to Moveable_Platform and make 2 duplicates of the platform.

Next, I’ll rename the 2 duplicates to Target_A and Target_B. These will serve as the two points that the platform will move between, so I’ll move them to where I want to platform to go.

Now I’ll select Target_A and target_B and remove their Mesh Filter, Mesh Renderer, and Box Collider since I only need their Transform component.

Next, I’ll create a script and name it MovingPlatform. Finally, I’ll attach this script to the Moveable_Platform object. This script will control the movement of the platform between Target_A and Target_B through code.

To have the platform move between Target_A and target_B, I’ll need to get a reference to those two points. To do this, I’ll create a Transform variable for each point and Serialize them so I can drag them into the Inspector.

Because I’m dealing with physics movement, I will do the calculations and movement in a FixedUpdate() method instead of Update(). This will provide a more measured and consistent timing to the movements involving physics. I’ll also use a unity method called MoveTowards() to have the platform move to each point.

I only want to platform to switch directions when it reaches each respective point. So I’ll create a private bool named _switching and use that to check if the platform has reached a point and if so, I’ll send it in the other direction and change the state of _switching.

So first, I set up the moving code based on whether _switching is true or false.

Then I add conditionals to change the value of _switching when the platform reaches either target.

This gives us a nice consistent moving platform.

That’s it for Part 1 of the article dealing with the moving platform. In Part 2, I’ll take care of a little problem when the Player actually jumps onto the moving platform.

I hope you found this interesting and informative. And until next time, I wish you all the best on your coding journey.



Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store