2.5D Platformer — Moving Platforms #5
Making modular platforms with ease!
What is a Platformer without at least some moving platforms? Not a good one, that’s for sure. So, I’ve implemented Modular Moving Platforms in my project, and they are so easy to make that you can too!
Create an empty GameObject and name it “MovingPlatform”. Inside this GameObject, we’ll add the actual platform (a reshaped cube will do just fine), and another empty GameObject for the platform travel points.
For the platform, you’ll want to add two box colliders (or one if it already has the default Box collider),one Rigidbody, and a new script called “MovingPlatform”. The extra collider will be a trigger and it will be useful to stick the player controller to the platform.
The platform needs three variables:
- A List to add and move between Travel Points
- A float variable to set the speed of the platform
- A integer variable to keep track of the current travel point the platform is moving towards.
I used the Start method to set the initial position of the platform to the first travel point in the list, and I used FixedUpdate to move the platform. Why not Update()? If you’re dealing with certain physical objects, it is best that you move those objects through FixedUpdate(), as it calculates any physic interactions and it will remove any jittery movement in the object.
The movement will check if it’s close to a certain distance threshold from the current travel point, and if it is, it will switch to the next Travel Point. If it’s not close, it will move towards that travel point.
With this, I have a moving platform! However, my controller doesn’t stick to the platform.
Sticking the controller to the plaform
Something fairly easy to do is, with use of that trigger collider I set earlier, is to parent the controller to it when it enters, and unparent it when the player exits it.
Bonus: Visualize the route
As a programmer / designer, I should make it easier to visualize the moving platform routes for my team and myself.
By using Gizmos, I can draw spheres and lines which will help me visualize and design a proper route.