Search Unity

Question Fixing parented rigidbodies not moving together

Discussion in '2D' started by Magnesium, May 29, 2023.

  1. Magnesium

    Magnesium

    Joined:
    Sep 14, 2014
    Posts:
    179
    Hello,

    I know this is a frequent issue but haven't found a solution on Google so far. I'm coding a 2d platformer and the character needs to be linked to moving platforms he's standing on. To do this, i set the character as a child of the platform and this used to work well. But since i was moving my plaftorm using it's transform property, it sometimes caused huge collision issues, which is expected.

    So i changed my code to use the platform's rigidbody MovePosition. Now collisions works fine, but the character no longer moves with the platform, which again is expected because that's how Unity's physics engine works.

    Now i'm looking for a way to fix this. I've tried setting the player's rigidbody as kinematic when it is parented but it doesn't work because i use velocity to move it left and right. I've thought about adding the platform's velocity to the player's velocity but that would mess up the player's gravity computation which is still handled by it's own rigidbody. I tried using the platform's velocity and add it to the player's transform property but it gave unexpected results.

    What would be the best solution to solve this?

    Thanks
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,745
    This stuff is always fiddly to get right. Enclosed is my Platform2D example.

    It uses an animator to move the platform.

    But you can change it to make the Animator drive the physics.

    It does not use Rigidbody2D to move the platform, but you can:

    - set the animator to drive physics
    - Add a Rigidbody2D to the platform
    - lock its rotation (if you don't it is hilarious... you might want to try it!)

    The animator struggles against your gravity in this case, but fundamentally the move / relative and anchor is operational.

    Perhaps @MelvMay can add more details... Melv knows this stuff cold.

    Screen Shot 2023-05-29 at 6.18.57 AM.png
     

    Attached Files:

  3. Magnesium

    Magnesium

    Joined:
    Sep 14, 2014
    Posts:
    179
    Thanks for your feedback. I never thought about using the animator component. I'll have to try it but it will require a severe amount of rewrite. But wouldn't that get me exactly the same result though? I'm guessing the animator will do the same thing my code does, manipulate the platforms physics and i'll run into the same issue of this physics not applying to my player. But that's just a guess.
     
  4. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,745
    The only reason I used an animator is that I try not to write code when an animator would suffice. :)

    I offer it only as an example. I recommend you rip off the animator in my example above and replace it with your .MovePosition() script and see what happens.

    As I said, this stuff can be quite fiddly, with every setup being slightly different, so I find it useful to have examples around to start banging on.
     
  5. Magnesium

    Magnesium

    Joined:
    Sep 14, 2014
    Posts:
    179
    Yeah no, i've just done a quick test, animator updated by physics seems to give the same result, the player no longer follows the platform.