Search Unity

Best way to get rigidbody position

Discussion in 'Physics' started by brett_ridel, Apr 13, 2018.

Thread Status:
Not open for further replies.
  1. brett_ridel

    brett_ridel

    Joined:
    Jan 15, 2017
    Posts:
    33
    Hi all

    I have a kinematic rigidbody with trigger collider.
    I move my rigidbody with MovePosition in FixedUpdate().

    However, sometimes I need to get (only get, not set) the position of my rigidbody.
    After some simple test, myrigidbody.transform.position seems to be more efficient than myrigidbody.position
    Can you confirm it ?

    Brett
     
  2. Edy

    Edy

    Joined:
    Jun 3, 2010
    Posts:
    2,510
    myrigidbody.transform.position is the interpolated position when your rigidbody has interpolation enabled. You may read it on each Update() call por providing visual effects that are in sync with the movement of the rigidbody.

    myrigidbody.position is the physics position, updated each physics step.

    If interpolation is not enabled then both positions contain the same value. I can't really tell about efficiency. You have to use one or other based on your specific case.
     
    jellevda, FariAnderson and StephanT like this.
  3. SparrowGS

    SparrowGS

    Joined:
    Apr 6, 2017
    Posts:
    2,536
    I did a lot of tests and rb.position is significantly more expensive then transform.position.
     
    FariAnderson likes this.
  4. FariAnderson

    FariAnderson

    Joined:
    Jan 20, 2020
    Posts:
    37
    you saved my life :) tnx.
     
    jellevda likes this.
Thread Status:
Not open for further replies.