Tip of the Day: Moving Platforms Unity 2D

Mohamed Hijazi
Nerd For Tech
Published in
3 min readApr 29, 2021

--

A very cool feature that you can always add to your 2D games is a moving platform. A platform that moves between two points either horizontally or vertically. Let’s see how to achieve it.

Objective: Moving Platforms

  1. Create the parent gameObject to hold the platform and the waypoints. Right Click > Create Empty.

2. Inside the parent object create a cube or any platform sprite you want and make sure it has two colliders and a rigidbody(make sure not to sue gravity or the platform will fall). One collider will allow the player to stand on, and set the second collider to trigger and make sure the trigger collider’s boundaries are above the platform so it can detect the player(will explain why in a bit).

A normal collider and another trigger collider
Trigger collider boundaries are above the platform

3. Create two empty objects inside the parent platform that will act as our waypoints.

Parent container that holds the moving platform and the waypoints.

4. Create a C# script and attach it to the child moving platform that will actually move. Serialize a speed value, and two transforms for the waypoints.

The code

We will depend on the Vector3.MoveTowards method that allows the platform to move from an original position to a target position with a defined speed. In order to change the target waypoint, we simply check if the platform is at waypoint A then the target is waypoint B and vice versa. For smooth movement, let’s use fixedUpdate().

step is the calculated speed

Finally a very important step. The trigger collider that we added, we will use it now to detect if player is on the platform or not. If he is on it, then we set the parent transform of the player to the platform, and if not, then we set it to null. This will ensure that when the player jumps on the platform, he stays on it and doesn’t fall down.

This is the result ===>

--

--

Mohamed Hijazi
Nerd For Tech

A knowledge seeking biologist who is following his passion into a full time career in Unity / Game Development. https://www.linkedin.com/in/mohamed-hijazi/