Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Rigidbody2D.MovePosition() when using non-kinematic dynamic rigidbodies

Discussion in 'Physics' started by RemDust, Jun 10, 2021.

  1. RemDust

    RemDust

    Joined:
    Aug 28, 2015
    Posts:
    431
    Hello guys,
    The documentation states that 2D.MovePosition is "intended for kinematic rigidbodies".
    What is the proper way to do it with non kinematic bodies ?
    I'm not looking for a force but an actual position movement (if that's even possible)

    Thanks !
     
  2. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    11,320
    Use it the same way. Saying it was intended for Kinematic should be easy to understand if you know that Kinematic means it's completely under your control. We also support it for Dynamic ("Non Kinematic" could be Static too). Tell it to move to a position and it moves, not much else to add.

    The odd thing about Dynamic is that you're not guaranteed to land at that position because Dynamic types react to other collisions. With Kinematic you won't have that problem.
     
  3. RemDust

    RemDust

    Joined:
    Aug 28, 2015
    Posts:
    431
    Hey @MelvMay , thank you for your quick reply.

    Here is the thing, I'm using a famous third party plugin for 2d animations, the guys from the plugin are working on a nice root motion solution but we found out that using it with dynamic rigidbody seem to raise some difficulties.

    To be specific, scaling the X axis movement of a character with MovePosition seem to have slightly different result depending on the gravityScale of the body (even without frictions or linear drag). That sounds weird and unexpected (as I don't see any force applied on the X axis with gravity working on the Y )!
    We are wondering how to tackle this.

    I'm no expert by no mean and I'm not the one working on the script so I'll be happy to bring somebody else if I haven't the ability to understand correctly ^^

    As always, thank you for your help :)
     
    Last edited: Jun 10, 2021
  4. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    11,320
    Because gravity is pushing down on the Y axis and the friction on a surface it's touching is slowing the X component of the velocity?

    This is the problem of doing this with MovePosition on a Dynamic body. All MovePosition does is turn off (temporarily) any current Linear/Angular Velocity/Drag, calculate the velocity to move to the selected position and then restore them. If you have gravity on that that'll be applied.

    A Kinematic body does not respond to forces whereas a Dynamic one does. If a Dynamic has its gravity scale set to zero and it doesn't contact anything then it'll produce identical results to Kinematic one.
     
  5. RemDust

    RemDust

    Joined:
    Aug 28, 2015
    Posts:
    431
    That was my guess before turning off all the linear and frictions on the rigidbody component :/
    I assume it would have get rid of the issue, right ?

    I totally get it, same old "position versus forces" story. But is there any other way to implement root motion ?
    Would calculate the distance first and then AddForce until you reach that position makes more sense than using MovePosition ?
     
  6. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    11,320
    Without knowing if that's the issue I cannot confirm that. This is why I tend to answer with "how it works" rather than what your solution is because I am not stating the reasons for what you're encountering. If friction is causing you to slow down (simply test) then turn it off (use a material with zero friction) because why would you need it if you're constanty issuing new positions? I mean, doing lots of opposing things then figuring out how to bypass them is making life hard for you for no reason I can see.

    In other words, why use Dynamic if you don't want dynamic reactions? If it's for gravity then just apply gravity to your body target position.
     
  7. RemDust

    RemDust

    Joined:
    Aug 28, 2015
    Posts:
    431
    I'm not sure how to do that, I would know how to apply a gravity-like force but if the rigidbody is kinematic how would you do that ?
    Also I don't want my enemies to go through walls, so it's not only about gravity.
     
  8. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    11,320
    Well use Dynamic then and turn off friction because you don't need it then all your problems will be solved right?
     
    RemDust likes this.
  9. RemDust

    RemDust

    Joined:
    Aug 28, 2015
    Posts:
    431
    THANK YOU !
    I don't know why I made the assumption that friction was zero by default if no rigidbody materials were set -_-
    The only marginal case I can think of is if my enemy walk on a non horizontal ground, I guess zero friction means he will be sliding. But it should be easily fixed by setting friction to zero during root motioned movement and reset to whatever friction works in usual movements.

    Thanks Melv :)