Search Unity

MovePosition not adjusting real rigidbody velocity

Discussion in 'Physics' started by edufireheart, May 16, 2019.

  1. edufireheart

    edufireheart

    Joined:
    Aug 6, 2017
    Posts:
    11
    As far as I knew, when using MovePosition in a non-kinematic rigidbody, it was supposed to do internal calculations to adjust the velocity of the rigidbody.
    I have the following piece of code inside FixedUpdate:
    Code (CSharp):
    1. Vector3 previousPosition = transform.position;
    2. Vector3 newPosition = GetBezierQuadraticCurvePoint((float)bezierParameter, originalPosition, curvePoint, finalPosition);
    3. deltaPosition = newPosition - transform.position;
    4. m_rigidbody.MovePosition(newPosition);
    5.  
    6. Vector3 currentPosition = transform.position;
    7. Vector3 actualDeltaPosition = currentPosition - previousPosition;
    8.  
    9. throwVelocity = actualDeltaPosition / Time.fixedDeltaTime;
    10. Debug.Log("difference of speeds: " + (throwVelocity - m_rigidbody.velocity).magnitude);
    11. throwVelocity = deltaPosition / Time.fixedDeltaTime;
    12. Debug.Log("difference of speeds: " + (throwVelocity - m_rigidbody.velocity).magnitude);
    GetBezierQuadraticCurvePoint() is just a function to calculate a point close to the current point following a Bezier curve. The real problem is that those two Debug.Log overthere show me that the difference between the rididbody.velocity to the one I should get from my own calculation is considerable, instead of being zero or close to that.
    What is it that is happening here with MovePosition?
     
  2. Edy

    Edy

    Joined:
    Jun 3, 2010
    Posts:
    2,510
    That happens in kinematic rigidbodies. In non-kinematic rigidbodies then both you and the physics are interring each other, so the results are uncertain. MovePosition forces the rigidbody to be at some position, but the velocity may have been already computed by the physics to be in a different position.
     
    edufireheart likes this.
  3. edufireheart

    edufireheart

    Joined:
    Aug 6, 2017
    Posts:
    11
    Thank you very much, I indeed had many problems trying to use the velocity of my rigidbody right after using MovePosition. I realized that it's just simpler to keep track of my previous and current position and use that whenever I need my velocity, since MovePosition won't force the immediate calculation of my current velocity, which will just be updated after a while (the next frame, probably)
     
    Edy likes this.