Search Unity

Moving ontop of moving-platforms has irregular speed

Discussion in '2D' started by Javi35, Jan 2, 2018.

Thread Status:
Not open for further replies.
  1. Javi35

    Javi35

    Joined:
    Jan 5, 2015
    Posts:
    2
    Heya, so im making a small 2d platformer and im runnnign into some issues when using moving platforms.

    Here's my setup, i got my player character with a Rigidbody2D and i move it by setting the the rbody velocity according to the inputs, pretty standard stuff.

    Since my character uses a Rigidbody i made my moving platform have a kinematic rigidbody and move it using Rigidbody2D.MovePosition. The platform moves accordingly and the player moves with it.

    My problem is that once i move the character ontop of it, its faster moving the opposite direction as the platform and much slower while walking the same direction as it. My first approach was to compensate for the plaer speed using the platform's, but its a kinematic rbody being moved with moveposition so... it doesnt have a speed.

    Any advice?
     
  2. methos5k

    methos5k

    Joined:
    Aug 3, 2015
    Posts:
    8,712
    Interesting question, that I've seen before, too.
    First off, I'd like to say that I've never completely solved this, or really had it in a game I was making.. :)
    I have tried a bit here n there, though.

    1 option is to make your player a child. This kinda works, depending on what you need.

    Just now, I was experimenting with an idea, and I'm not sure how good it is, but I created a simple game object with a dynamic rigidbody and collider and place it on the platform. I referenced this as its pet.
    Now, from my player script, I assign the pet to a variable when I enter its trigger (and unassign when I exit).
    Now, I can get the "pet"'s velocity, and use in conjunction with my player's movement to work out the velocity of the player.
    To be fair, I spent only about 5-10 mins on this, so there may be holes in my logic that I haven't yet thought of lol
    One obvious issue is you can't have that pet object interact with the player, for collisions. :)

    Anyways, I hope you get something that works for you. I'm curious to see what comes of this thread, your solution, and any suggestions that other people might have from experience.
     
    Vollmondum and Javi35 like this.
  3. Javi35

    Javi35

    Joined:
    Jan 5, 2015
    Posts:
    2
    Hey, thanks for taking the time to respond!

    I tried making the character a child of the platform and moving the platform with vector.movetowards but the movement becomes sort of slow and jittery. If i use moveposition with parenting, it doesnt stick to the platform at all.

    i read an article saying the parenting method works better when you're using a character controller rather than rigidbody-speed based movement, im considering chanign to that method of movement now.

    Regarding the pet approach, since its moving with either vector.movetowards or rigidbody.moveposition i dont think i have a speed/velocity value to correct my player's speed when walking ontop
     
  4. methos5k

    methos5k

    Joined:
    Aug 3, 2015
    Posts:
    8,712
    Hm. When I tried move position on the platform and the player as a child it worked for me. When you say it doesn't stick, what happened there? It fell off or something else?

    As for the pet idea, the pet is a dynamic rigidbody on the platform (platform is moved with move position), so you don't the velocity from the platform, but you get it from the pet :)
    At least, that's what I was saying..
     
    Vollmondum likes this.
  5. Vollmondum

    Vollmondum

    Joined:
    Sep 19, 2012
    Posts:
    10
    Maybe a bit outdated, but need to add the solution, which is simple as hell.
    The speed doesn't actually change. Your player keeps moving with normal speed across the platform.
    What you saw as problem was an optical illusion.
    When you move on the platform it seems you speed up while moving in direction the platform moves, but you don't. Same for backward direction, you don't slow down, just visually.
    Disabling background rendering helps to see it. Keep your player and the platform.
    You can also count the time while running from one end to the other, and you'll see it's the same both directions.
    Hope this helps to skip the visual issues for the others.
     
  6. MisterSkitz

    MisterSkitz

    Joined:
    Sep 2, 2015
    Posts:
    833
    Why not just make the player speed reduce whilst walking away from the motion of the object or speed him up whilst walking towards? All you'd need is a variable to track which direction the platform was moving and make it static. Then check inside an update on the player's movement script which direction it's going. Granted you can make an "isOnPlatform" variable and only run the checks in update if isOnPlatform is true. But as @Vollmondum said, it's really an optical illusion because you're either moving with or against, seemingly moving faster/slower. So what I suggest is to create your own illusion to compensate.

    Who knows? Maybe you could play around with the linear drag, mass, and other Rigidbody2D settings to better acquire your desired effect?
     
  7. anish_30

    anish_30

    Joined:
    Mar 10, 2022
    Posts:
    1
    Make sure the z transform for the player and platform does not have a large difference.
     
  8. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    11,500
    Please check the dates of posts before posting. This question was asked 4 years ago.

    Thanks.
     
Thread Status:
Not open for further replies.