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!

Setup

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.

Also, check the Rigidbody configuration. This is important if you don’t want to have any weird issues.

Code

Setup

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.

Moving

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.

Result

--

--

--

Junior Unity Developer

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

Let service teams own the service operations instead of the SRE

How to “learn submit” with style

How to get started with Firebase

Getting started with Svelte

A Comprehensive Guide of the Software Development Life Cycle

Engineering Activity Idea for Children

New Features Jenkins 2.249.3

Blog migrated to Statiq

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
Pablo Gómez Platón

Pablo Gómez Platón

Junior Unity Developer

More from Medium

2D Mobile: Setting Up The Player

Adding shields to some enemies

2D Player Animation (part 2)

Installing Unity and Getting Started with Game Development