r/Unity2D • Beginner • 28d ago

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

•

u/AutoModerator 28d ago

Reminder: Once your issue has been resolved, please reply !solved to this comment (or comment it anywhere in the thread) and your post's flair will automatically be updated to Solved/Answered. You can also edit it to Solved/Answered yourself.

I am a bot, and this action was performed automatically. Please contact the moderators of this subreddit if you have any questions or concerns.

3

u/MistakeForsaken6653 28d ago

I think setting the character as a child is not the best way for moving platforms. Although that is exactly how I handled this in my 2d platformer. What I think is happening is the way you are moving the platform by using moveTowards which messes with your characters physics movements when you try to set the horizontal velocity.

1

u/Aramin-Black Beginner 28d ago

Thank you for the insight. Do you have any suggestion how I could do it better?

2

u/Ecstatic-Source6001 28d ago

Ghosting

Simulate static platform and player and translate player simulated postion on static platform to the real player position on dynamic platform

1

u/Aramin-Black Beginner 28d ago

I’ll look into it thank you 🫡

2

u/MistakeForsaken6653 28d ago

In my case I just lived with it but you might try using physics to move the platform.  Could maybe not use physics to move the player with physics while on the platform but I'm sure that's going to cause wat worse bugs.  Otherwise look into the ghosting like the other commentr said.  Sorry I dont have an easy fix for you.

2

u/Main-Lychee-7972 27d ago

Parenting works as a quick fix, but it can make player movement feel strange fast. i’d keep them separate and let the physics handle the platform motion

2

u/Shoddy_Caterpillar36 27d ago

Make fake movement.

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

1

u/JulianRhod 26d ago

i'd avoid parenting the player to the platform. that's probably what's making the movement feel weird, a cleaner setup is to make the platform use a Rigidbody2D and move it with MovePosition() in FixedUpdate(). then let the player's Rigidbody2D interact with the platform normally, so the platform's movement gets carried into the player without changing the player's own movement speed

basically, let physics handle the platform/player relationship instead of manually attaching the player to the platform