Search Unity

Moving a Kinematic Rigidbody

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

  1. AndorfTheWhite

    AndorfTheWhite

    Joined:
    Nov 1, 2015
    Posts:
    51
    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
     
  2. csofranz

    csofranz

    Joined:
    Apr 29, 2017
    Posts:
    706
    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. AndorfTheWhite

    AndorfTheWhite

    Joined:
    Nov 1, 2015
    Posts:
    51
    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.
     
  4. csofranz

    csofranz

    Joined:
    Apr 29, 2017
    Posts:
    706
    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. AndorfTheWhite

    AndorfTheWhite

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