Search Unity

Differences between transform.Translate, rigidbody.Velocity and rigidbody.MovePosition?

Discussion in 'Physics' started by vertig02, Jun 26, 2018.

  1. vertig02

    vertig02

    Joined:
    Nov 30, 2017
    Posts:
    61
    So i'm relatively new to Unity and i'm currently playing around with movement of physical objects, and I found these three Script methods, is there any difference between them and which one would be best for which situation if they are different?
     
  2. xVergilx

    xVergilx

    Joined:
    Dec 22, 2014
    Posts:
    3,296
    Transform is not a physics based component. It's position, rotation and scale of the object.
    It doesn't interact with physics system on it's own.

    Transform.Translate - moves the transform component ignoring physics.

    Rigidbody is physics based component that performs different kinds of physics interactions, like collisions, depenetration, forces and more.

    Rigidbody.velocity - you shouldn't modify directly.
    You should use Rigidbody.AddForce / Rigidbody.AddTorque.
    It is used to get/set to directly pull the direction/velocity from rigidbody bypassing physics calculations done internally.

    Rigidbody.MovePosition - it attempts to move the object to the specific world space position, by interpolating and calculating forces on it's own.

    So they are different.
    There's drastically more than this, this is just a dumbed down simplified explanation.
    If you want to learn more I suggest starting with Scripting API / Manual. There's plenty of info there.
     
    Last edited: Jun 28, 2019
  3. vertig02

    vertig02

    Joined:
    Nov 30, 2017
    Posts:
    61
    Just did some research, amd i got a little confused about .MovePosition, because it stated there that it only works when you set isKinematic to false, otherwise it just teleports. MovePosition supposedly takes Phtsics into account, but when isKinematic is false the RigidBody is not affected by Physics anymore, so won't that mean it would be the same as .Translate?
     
  4. xVergilx

    xVergilx

    Joined:
    Dec 22, 2014
    Posts:
    3,296
    When isKinematic is set to true - it does indeed teleports right away. But then again, it's designed that way.
    If isKinematic is set to false, it will attempt to resolve any collisions, and provides interpolation based on settings.
    It's really useful if you want to move your rigidbodies with something like Vector3.MoveTowards instead of calculating forces etc.

    Make sure you're understanding isKinematic correctly.
    Simplified:
    isKinematic == true == no physics, Rigidbody is script driven.
    isKinematic == false == Rigidbody is physics interaction driven.
     
    et3rnald and vertig02 like this.
  5. vertig02

    vertig02

    Joined:
    Nov 30, 2017
    Posts:
    61
    Bluh, i mixed up the isKinematic states, sorry about that, what i meant to say was that the docs say that the .MovePosition only works with isKinematic set to true, but if it is set to true it would mean the rigidbody would not be affected by Physics, which was why i got confused
     
  6. vertig02

    vertig02

    Joined:
    Nov 30, 2017
    Posts:
    61
    So i just experimented with all of them and the results are: transform.Translate does exactly as you said, phasing through walls when moving, though sometimes the RigidBody does complain; rigidbody.AddForce makes the game object roll like a ball (Understandable because i attached a Capsule shaped Collider), but the MovePosition is kind of glitchy. When called it doesn't move where i want it to move (Which transform.forward), and instead teleports the game object right to the border of the map and sometimes even out of the world, into the void. Doesn't matter what i do, it always ends up in that one corner outside the map when the function is called, which is weird since i never made it do that
     
  7. xVergilx

    xVergilx

    Joined:
    Dec 22, 2014
    Posts:
    3,296
    Rigidbody.MovePosition() takes position in world space as parameter, not direction.
    World space position
    is basically what transform.position is.

    To move smootly via .MovePosition() you can do following:
    Code (CSharp):
    1. Vector3 destination; // Where to move, in world space.
    2. Vector3 currentPos; // Could be transform.position
    3. float moveSpeed; // gotta go fast
    4.  
    5. private void FixedUpdate() {
    6.    Vector3 smoothedDelta = Vector3.MoveTowards(currentPos, destination, Time.fixedDeltaTime * moveSpeed);
    7.  
    8.    // Then apply it to the rigidbody:
    9.    Rigidbody.MovePosition(smoothedDelta);
    10. }
     
    Last edited: Jun 27, 2018