Search Unity

Rigidbody.MovePosition - Not working as expected?

Discussion in 'Physics' started by wantondalliance, Oct 10, 2019.

  1. wantondalliance

    wantondalliance

    Joined:
    Aug 22, 2018
    Posts:
    34
    One cube, with a rigidbody and two scripts attached:

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class MoveRigidbodyDown : MonoBehaviour
    6. {
    7.     Rigidbody _rb;
    8.  
    9.     // Start is called before the first frame update
    10.     void Start()
    11.     {
    12.         _rb = GetComponent<Rigidbody>();
    13.     }
    14.  
    15.     // Update is called once per frame
    16.     void FixedUpdate()
    17.     {
    18.         _rb.MovePosition(_rb.position + Vector3.up * -1);
    19.     }
    20. }
    21.  
    and

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class MoveRigidbodyUp : MonoBehaviour
    6. using UnityEngine;
    7.  
    8. public class MoveRigidbodyUp : MonoBehaviour
    9. {
    10.     Rigidbody _rb;
    11.  
    12.     // Start is called before the first frame update
    13.     void Start()
    14.     {
    15.         _rb = GetComponent<Rigidbody>();
    16.     }
    17.  
    18.     // Update is called once per frame
    19.     void FixedUpdate()
    20.     {
    21.         _rb.MovePosition(_rb.position + Vector3.up * 1);
    22.     }
    23. }
    The result:



    I would expect the cube to get moved up and then down, or down and then up, depending on script order - therefore leaving the cube where it is. But I'm finding that whichever script is executed last (out of these two) gets executed and the other one seems to get ignored. Am I missing some crucial aspect of this function?

    (It was happening in both 2019.2 + 2019.3 beta)
     
    Last edited: Oct 10, 2019
  2. Edy

    Edy

    Joined:
    Jun 3, 2010
    Posts:
    2,512
    Rigidbody.MovePosition just overrides the result of the physics calculations for the current fixed timestep. As you're calling MovePosition twice in the same timestep, then only the latest call applies.
     
  3. wantondalliance

    wantondalliance

    Joined:
    Aug 22, 2018
    Posts:
    34
    A friend tried the same thing out, but their rigidbody stays still. Physics settings are all the same. How can this be?

     
  4. Edy

    Edy

    Joined:
    Jun 3, 2010
    Posts:
    2,512
    Because two MovePosition calls within the same timestep result in the rigidbody using only the latest value. Although the calls are in different scripts, both FixedUpdate events are invoked within the same timestep.
     
  5. wantondalliance

    wantondalliance

    Joined:
    Aug 22, 2018
    Posts:
    34
    @Edy that makes sense, but can you see any reason why my friend would be getting different results? As you can see, it seems to be considering both values and cancelling eachother out.