Moving Platform in Unity: Best Beginner Guide

Moving platform in unity add dynamic elements to game environments, enhancing player experience and creating challenging scenarios. In Unity 2D, creating moving platforms requires thoughtful design and precise implementation. In this tutorial, we will explore a step-by-step approach to building moving platforms with a focus on customizability and player interaction.

Moving Platform in Unity

Moving Platform In Unity:

These are the steps which will help you in making moving platform in unity 2d, by following them you’ll be able to make a moving platform which will move back and forth between as many way points as you like and the player will also stick to the platform and won’t slide off without any bugs.

1. Setting Up the Platform:

To begin, create a GameObject in Unity and assign a sprite to it. Attach a BoxCollider component to define the platform’s physical boundaries.

attach box collider 2d

While a simple Rigidbody approach can create basic movement, like we can use the Rigidbody velocity property to move the platform and then change its direction after a number of seconds.

Learn About Rigidbodies In Unity From Here

2. Using Waypoints for Customization:

The key to creating versatile moving platforms lies in using waypoints. Make two empty GameObjects, positioning them a few units apart.

empty gameobject named waypoints placed further apart

And then keep all three of the gameObjects under an empty parent gameObject,

place everything under one parent gameObject

Keep the y-axis values the same for simplicity. Then, create another empty GameObject and make it the parent of the platform sprite and the two waypoints.

3. Writing the “FollowWaypoint” Script:

Create a script named “FollowWaypoint” and attach it to the platform GameObject. Inside the script.

make follow waypoint script

Declare an array named “waypoints” and two integer variables, “currentWaypointIndex” and “speed.” This allows for the flexibility to add as many waypoints as needed.

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class FollowWaypoint : MonoBehaviour
{
    [SerializeField] private GameObject[] waypoints;
    private int currentWaypointIndex;
    [SerializeField] private int speed;

    // Update is called once per frame
    void Update()
    {
        
    }
}

4. Implementing Waypoint Movement:

In the Update method of the script, create an if statement to check if the distance between the platform and the current waypoint is less than 0.1f. If true, increment the currentWaypointIndex. Add another if statement to reset the index to 0 if it becomes greater than or equal to the array length.

void Update()
{
    if (Vector2.Distance(waypoints[currentWaypointIndex].transform.position, transform.position) < 0.1f)
    {
        currentWaypointIndex++;
        if (currentWaypointIndex >= waypoints.Length)
        {
            currentWaypointIndex = 0;
        }
    }
}

Finally, use Vector2.MoveTowards to move the platform smoothly between waypoints.

void Update()
{
    if (Vector2.Distance(waypoints[currentWaypointIndex].transform.position, transform.position) < 0.1f)
    {
        currentWaypointIndex++;
        if (currentWaypointIndex >= waypoints.Length)
        {
            currentWaypointIndex = 0;
        }
    }
    transform.position = Vector2.MoveTowards(transform.position, waypoints[currentWaypointIndex].transform.position, Time.deltaTime * speed);
}

Now, if you try to step on the moving platform in unity, you will be able to stand on it but you will slide off and not move with the platform

5. Handling Player Interaction:

To prevent the player from sliding off the platform, add two BoxCollider components. Arrange them as shown, making the smaller one on top a trigger. This setup will help in sticking the player to the platform.

attach two box colliders

Enable the top colliders “Is Trigger” option.

box collider properties

6. Creating the “StickyPlatform” Script:

Build a script named “StickyPlatform” and attach it to the platform GameObject.

create sticky platform script

In the OnTriggerEnter2D method, check if the colliding GameObject is the player. If true, set the player’s parent to the platform’s transform. In the OnTriggerExit2D method, set the parent back to null to release the player.

public class StickyPlatform : MonoBehaviour
{
    private string playerName = "Player";
    private void OnTriggerEnter2D(Collider2D collision)
    {
        if(collision.gameObject.name == playerName)
        {
            collision.gameObject.transform.SetParent(transform);    
        }
    }

    private void OnTriggerExit2D(Collider2D collision)
    {
        if (collision.gameObject.name == playerName)
        {
            collision.gameObject.transform.SetParent(null);
        }
    }
}

Read This Article To Learn Player Death Logic In Unity 2D To Polish Your Game Dev Skills:

FAQs

What is the “is Trigger” option in the Box Collider?

The “is Trigger” option in the Box Collider allows it to act as a trigger zone rather than a physical obstacle. This is essential for detecting interactions without affecting the platform’s physical properties.

How do I keep a player on moving platform in unity?

To keep a player on a moving platform in Unity, you can implement a script that ensures the player moves with the platform. Use OnTriggerEnter2D and OnTriggerExit2D to set and release the player’s parent to the platform’s transform, respectively. This way, the player sticks to the moving platform in unity, preventing undesired sliding or falling off.

Why use waypoints for moving platforms?

Waypoints provide a flexible and customizable approach to platform movement. By setting up designated points, you can control the platform’s path, direction, and speed, enhancing the overall gaming experience.

Is it possible to add more waypoints to the platform’s movement path?

Absolutely. The “FollowWaypoint” script is designed to handle an array of waypoints. You can easily add as many waypoints as desired, creating diverse and dynamic movement patterns for your Unity 2D game.

How do I troubleshoot if the player is not sticking to the platform?

Ensure that the Box Collider components are set up correctly, especially the trigger collider. Double-check the “StickyPlatform” script for accurate implementation in the OnTriggerEnter2D and OnTriggerExit2D methods.

Conclusion:

By following these steps, you can create moving platform in Unity 2D that offer both customization and player interaction. The waypoint system allows for dynamic movement patterns, and the addition of the “StickyPlatform” script ensures that the player just doesn’t slide and fall off the platform resulting in frustrating players. Experiment with different waypoint arrangements and customize the movement speed to enhance the gameplay in your Unity 2D platformer.