Search Unity

Moving a Kinematic Rigidbody

Discussion in 'Scripting' started by Terminus001, Jul 22, 2019.

  1. Terminus001

    Terminus001

    Joined:
    Nov 1, 2015
    Posts:
    121
    Hi Everyone,

    I want to move a gameobject whci has a kinematic rigidbody and a collider, but Unity seems to contradict itself when it comes to methods used to move the gameobject:

    1. https://docs.unity3d.com/Manual/CollidersOverview.html
    2. https://www.coursera.org/lecture/pr.../learn/programming-level-design?action=enroll (at minute 0:41)

    Source 1 states "You can move a kinematic rigidbody object from a script by modifying its Transform Component [...].". Source 2 states "You can move it (kinematic rigidbody) around independent of gravity using of course the Rigidbody methods [...].".

    I saw online threads where people state both. Personally, I would use the Rigidbody methods inside a FixedUpdate() in order to allow complete physics calculations for interactions with other gameobjects.

    Please let me know what would be right and wrong.

    Thank you in advance,

    mitoand9
     
    kbrizov likes this.
  2. csofranz

    csofranz

    Joined:
    Apr 29, 2017
    Posts:
    1,556
    You can move any object around by directly manipulating its transform, with or without RB. If you directly manipulate the transform, do it in Update() (the non-fixed one).

    If you have an RB attached, the object can have additional transformations applied by the RB, depending on useGravity and isKinematic.

    If you disable isKinematic, you can also use the Physics engine to move an object with RB by applying force. You should do this in FixedUpdate.

    So it's not an (either or), it's the (one or both or) that applies here. Adding an RB will open additional methods of controlling your object. They may not always interact nicely, though. I usually end up using the RB/FixedUpdate to turn my objects (because I have a nice PID with super cool smoothing set up), but move the object using absolute positioning with transform (in Update).
     
  3. Terminus001

    Terminus001

    Joined:
    Nov 1, 2015
    Posts:
    121
    Alright I see, I understand that most times it's a matter of what is convenient in a specific situation. But for example, the Unity intructor said, quote: "You don't want to move around a Kinematic Rigidbody using its transform node because that's going to introduce some fighting with it and the Rigidbody. That may not seem like a huge deal, it's going to look bad, but the fact is that if anything touches it, the force are going to get applied to the other Rigidbody and you could send a character (for example) flying across the screen because you suddenly accelerated very quickly over one frame."

    This statement seems to suggest that one should always use Rigidbody methods instead of Transform methods when dealing with any type of Rigidbody movement.
     
    kbrizov likes this.
  4. csofranz

    csofranz

    Joined:
    Apr 29, 2017
    Posts:
    1,556
    It's a good rule of thumb, yes. if you can, do. You'll reap the benefits of having nice interaction with almost any other physics enabled game object. However, even Unity acknowledges that it won't always work that way - or they wouldn't have introduced the isKinematic flag, which swithces off physiscs. It's like an Italian friend of mine regards traffic lights. To him, a red light merely suggests he stops his vehicle, and the speed limit is more a guideline than anything else. It's perilous to disregard them, and you do it at your own risk, but you may reach your goal quicker. Or end up in the morgue..
     
  5. Terminus001

    Terminus001

    Joined:
    Nov 1, 2015
    Posts:
    121
    Ahahaah I see, I'm not that kind of Italian, but I get your point. Thanks for the help!
     
  6. kbrizov

    kbrizov

    Joined:
    Sep 12, 2015
    Posts:
    2
    This is really unfortunate, but the documentation indeed contradicts itself. This is driving me mad. And no, there should not be a "rule of thumb". There should be a proper way and a wrong way. The physics engine is complex. It does all kinds of optimizations. Having 1000 ways of manipulating a kinematic rigidbody is such a pain.

    The RigidBody2D.MovePosition documentation says it's supposed to be used with Kinematic bodies.
    The RigidBody.IsKinematic documentation says that rigidbodies should be manipulated via transform.position.

    Someone needs to fix this...
     
  7. unity_bvs3f_C6bO_T7g

    unity_bvs3f_C6bO_T7g

    Joined:
    Mar 23, 2019
    Posts:
    1
    As far as I know it's because they are different implementation between 2d/3d physics. It's Box2D for 2D and nvidia physx for 3D.
     
    Connorses likes this.