Search Unity

Differences between object movement functions

Discussion in 'Physics' started by Koval331, Feb 27, 2019.

  1. Koval331

    Koval331

    Joined:
    Feb 3, 2019
    Posts:
    114
    So I was trying to understand the differences between different ways to move an object in Unity and got opposite answers in different places. Here is what I was able to figure out though. I would ask you to check and correct me if I am wrong at any of them:
    • transform.Translate has nothing to do with the physics. It simply teleports the object from point A to point B.
    • MovePosition for kinematic bodies slides (instead of teleporting) the object from point A to B + interpolates the object position in the frames to be rendered in-between FixedUpdate calls.
    • MovePosition for non-kinematic bodies ( aka dynamic ) teleports them from point A to point B.
     
  2. Edy

    Edy

    Joined:
    Jun 3, 2010
    Posts:
    2,510
    Two first points are correct. Third point is not correct. MovePosition for non-kinematic bodies also slides the object, but Physics is also trying to slide the object based on physics simulation. The MovePosition call prevails, but if interpolation is enabled then the object will stutter visually because MovePosition and Physics enforcing different movements.

    EDIT: It's also significant that transform.Translate ignores any physics considerations, while MovePosition causes physically correct effects in other physics objects.
     
  3. Koval331

    Koval331

    Joined:
    Feb 3, 2019
    Posts:
    114
    Ok. Thanks! Then I guess the text below the code example on this page that says the opposite: "If the rigidbody has isKinematic set false then it works differently. It works like transform.position=newPosition and teleports the object" is wrong, right?

    And when you say "the MovePosition prevails" do you mean that it is better? ( English isn't my native :) )
     
    Last edited: Feb 28, 2019
  4. Koval331

    Koval331

    Joined:
    Feb 3, 2019
    Posts:
    114
    Also, I can see a little bit of jerkiness in my kinematic object's movement when I use transform.Translate. Do you think MovePosition will solve it? Or maybe Rigidbody.AddForce is better? Thanks in advance.
     
  5. Edy

    Edy

    Joined:
    Jun 3, 2010
    Posts:
    2,510
    Rigidbody.AddForce doesn't have effect on kinematic rigidbodies. If you translate them with transform.Translate then you may be interfering with Unity's interpolation. MovePosition will solve it, but the choice here depends on whether you want realistic physics effects in other objects (i.e. carrying a non-kinetimatic object on top of your kinematic object) or not.

    Here are the two choices for moving a kinematic rigidbody summarized:
    • transform.Translate: do it from Update and ensure that Interpolation is disabled. No realistic physics effects on other objects: they will only be pushed away as the physics prevents the inter-penetration (remember we're teleporting the object, even at small intervals).
    • MovePosition: do it from FixedUpdate and ensure that Interpolation is enabled. Object is physically "slided". Realistic physics effects on other objects.