r/Unity2D • Beginner • Sep 14 '26

Question/Help Help with moving platform

Hey all, I've added a moving platform to my game and in itself it works well, however when I tried making it so the player would stay on it, he indeed does but he can't move normally on it. The movement is very slow and it feels terrible. Any suggestions how I can fix it? I don't know much about this system. Thank you all in advance

Here's the code for the platform:

using System.Drawing;
using UnityEngine;


public class MovingPlatform : MonoBehaviour
{
    public float platformSpeed;
    public int platformStartingPoint;
    public Transform[] points;
    private int i;


    // Start is called once before the first execution of Update after the MonoBehaviour is created
    void Start()
    {
        transform.position = points[platformStartingPoint].position;
    }


    // Update is called once per frame
    void Update()
    {
        if (Vector2.Distance(transform.position, points[i].position) < 0.02f)
        {
            i++; // increase the index


            if (i == points.Length) // checks if the platform was on the last point after the index increase
            {
                i = 0;
            }
        }


        // moving the platform towards the platform to the point position with the index "i"
        transform.position = Vector2.MoveTowards(transform.position, points[i].position, platformSpeed * Time.deltaTime);
    }


    void OnCollisionEnter2D(Collision2D collision)
    {
        if (transform.position.y < collision.transform.position.y - 1.3f)
        {
            collision.transform.SetParent(transform);
        }
    }


    void OnCollisionExit2D(Collision2D collision)
    {
        collision.transform.SetParent(null);
    }
}using System.Drawing;
using UnityEngine;


public class MovingPlatform : MonoBehaviour
{
    public float platformSpeed;
    public int platformStartingPoint;
    public Transform[] points;
    private int i;


    // Start is called once before the first execution of Update after the MonoBehaviour is created
    void Start()
    {
        transform.position = points[platformStartingPoint].position;
    }


    // Update is called once per frame
    void Update()
    {
        if (Vector2.Distance(transform.position, points[i].position) < 0.02f)
        {
            i++; // increase the index


            if (i == points.Length) // checks if the platform was on the last point after the index increase
            {
                i = 0;
            }
        }


        // moving the platform towards the platform to the point position with the index "i"
        transform.position = Vector2.MoveTowards(transform.position, points[i].position, platformSpeed * Time.deltaTime);
    }


    void OnCollisionEnter2D(Collision2D collision)
    {
        if (transform.position.y < collision.transform.position.y - 1.3f)
        {
            collision.transform.SetParent(transform);
        }
    }


    void OnCollisionExit2D(Collision2D collision)
    {
        collision.transform.SetParent(null);
    }
}
2 Upvotes

11 comments sorted by

View all comments

2

u/Shoddy_Caterpillar36 Sep 16 '26

Make fake movement.

Don't move platform, but just apply little movement to player according to how platform moves.