Search Unity

RigidBody.Velocity VS RigidBody.MovePosition

Discussion in 'Physics' started by folkeg, Sep 27, 2016.

  1. folkeg

    folkeg

    Joined:
    Sep 15, 2016
    Posts:
    6
    Hi I am asking a very basic question:
    Is setting velocity:
    playerRigidBody.velocity=newVector3(horizontalMove*speed,0.0f,verticalMove*speed);

    As same as:
    movement.Set (horizontalMove, 0.0f, verticalMove);
    movement = movement.normalized * speed * Time.deltaTime;
    playerRigidBody.MovePosition (playerRigidBody.position + movement);


    Thanks!
     
    JayGarxP likes this.
  2. Edy

    Edy

    Joined:
    Jun 3, 2010
    Posts:
    1,548
    Those are mostly equivalent when used with the correct type of rigidbody:
    • Setting velocity requires a non-kinematic rigidbody. When the velocity is applied, the rigidbody moves as driven by the physics simulation normally.
    • MovePosition works the same when the rigidbody is kinematic. In that case, you set the position on each fixed frame and the velocity gets calculated internally.
    Using MovePosition on a non-kinematic rigidbody may have issues as both you and the physics are specifying the position of the rb at the same time. Setting velocity on a kinematic rigidbody doesn't cause any effect.
     
  3. JamesLeeNZ

    JamesLeeNZ

    Joined:
    Nov 15, 2011
    Posts:
    5,618
    MovePosition doesnt use/set velocity at all. We've had this discussion previously.

    As a caveat, it may do it internally, but Velocity will read as zero.
     
    saneangel8 likes this.
  4. Edy

    Edy

    Joined:
    Jun 3, 2010
    Posts:
    1,548
    Fact: calling MovePosition and MoveRotation on each FixedUpdate on a kinematic rigidbody causes both rb.velocity and rb.angularVelocity to get updated with the actual velocity of movement.
     
  5. JamesLeeNZ

    JamesLeeNZ

    Joined:
    Nov 15, 2011
    Posts:
    5,618
    never mind. we are talking about different rb kinematics...
     
  6. JamesLeeNZ

    JamesLeeNZ

    Joined:
    Nov 15, 2011
    Posts:
    5,618
  7. Edy

    Edy

    Joined:
    Jun 3, 2010
    Posts:
    1,548
    Oh! Yes, I had missed that conversation.
     
  8. hexagonius

    hexagonius

    Joined:
    Mar 26, 2013
    Posts:
    96
    Just to finally understand this:

    - MovePosition and MoveRotation are used in FixedUpdate on kinematic rigidbodies exclusively without changing their actual angular- and velocity
    - Setting angular- and velocity in FixedUpdate will to the same for non-kinematic rigidbodies exclusively
     
  9. Edy

    Edy

    Joined:
    Jun 3, 2010
    Posts:
    1,548
    - Calling MovePosition and MoveRotation on a kinematic rigidbody in each FixedUpdate frame will cause its linear and angular velocity to be calculated and exposed (Rigidbody.velocity and Rigidbody.angularVelocity).
    - Setting velocity and angular velocity on a non-kinematic rigidbody causes an instant velocity change in that rigidbody. I think it's the same as calling AddForce with ForceMode.VelocityChange.
     
    Malbers and Sungold like this.
  10. BroncoBilli

    BroncoBilli

    Joined:
    Oct 8, 2017
    Posts:
    48
    Old thread! Current topic! (for me)

    I have a rigged hand that follows my motion controllers, each finger bone has a twin, a following object that follows the original bone. The following object has both a capsule collider on it, and a (kinematic) RB. The game has a ball in it, with both a collider and a RB. The ball's RB's mass is set up to be 1. the hand's finger bone follower objects, their RB's masses are 100. In FixedUpdate, I call the follower's RB object with MovePosition and MoveRotation. You can see in the physics debugger window, the follower objects and colliders are moving along with the finger bones. However, when the hand smacks the ball, the ball moves VERY slowly. it's as if the internal velocity that is being set on the RB is VERY small. And in fact, it is. If I get the velocity of the RB, right after calling MovePosition and MoveRotation, AND I calculate the velocity using the current position and the last position and the deltaTime, the calculated velocity is 10000x larger in size. (0.02 instead of 0.000002). The scales of everything in the hand and the ball are '1'. The velocity I'm getting back from querying the hand bones cannot be correct ... 0.000002 in the x direction can't possibly be the velocity, that's at full swing speed.

    somebody suggested the ROOT of my hand, only, should have a RB and not each finger bone. But if I'm using a follower system, it would seem like each finger bone should have its own RB and collider. Right?
     
  11. TheJavierD

    TheJavierD

    Joined:
    Jan 6, 2017
    Posts:
    18
    I've also been relying on this automatic calculation of velocities of kinematic rigidbodies with MovePosition/Rotation

    The problem is that it's not reliable, if performance isn't great or the timestep is too small (say 0.01) most of the time the calculated velocities will be 0.
     
  12. Edy

    Edy

    Joined:
    Jun 3, 2010
    Posts:
    1,548
    I've found it to be perfectly reliable, no matter the performance or the time step. If calculated velocities are 0 that means that MovePosition and MoveRotation are feed twice with the same value in two consecutive fixed time steps. Maybe you're updating your values from Update instead of FixedUpdate?
     
  13. TheJavierD

    TheJavierD

    Joined:
    Jan 6, 2017
    Posts:
    18
    Thanks Edy, you helped me out, much appreciated
     
    Edy likes this.