Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

rigidbody.MovePosition laggy

Discussion in 'Physics' started by demozbox, Jun 22, 2021.

  1. demozbox

    demozbox

    Joined:
    Nov 26, 2014
    Posts:
    83
    Hi, I am using rigidbody movePosition for movig player and it looks very laggy. The documentation says that enable Interpolate is required on rigidbody inspector for smooth moving but it doesn't fix the issue.
    Camera follow the player with Vector3.Lerp or Vector3.SmoothDump and I see smooth translation of the environment on the screen but body is very stuttering.
    Is there solution for the issue?
    The moving body class:
    Code (CSharp):
    1. public class TestMovePong : MonoBehaviour
    2. {
    3.     [SerializeField] private Rigidbody _rigidbody = default;
    4.  
    5.     private float _currentSpeed = 15f;
    6.     private Vector3 _direction = Vector3.right;
    7.     private float _pong;
    8.  
    9.     private void Update()
    10.     {
    11.         _pong = Mathf.PingPong(Time.time, 2);
    12.         _pong -= 1;
    13.     }
    14.  
    15.     private void FixedUpdate()
    16.     {
    17.         _rigidbody.MovePosition(transform.position + _direction * (_pong * Time.fixedDeltaTime * _currentSpeed));
    18.     }
    19. }
    Moving camera class:
    Code (CSharp):
    1. public class TestFollowTarget : MonoBehaviour
    2. {
    3.     [SerializeField] private Transform _target = default;
    4.     [SerializeField] private float _currentSpeed = 10f;
    5.     [SerializeField] private float _lerpTime = 1;
    6.  
    7.     private Vector3 _moveLerp;
    8.     private float _zOffset;
    9.  
    10.     private void Start() =>
    11.         _zOffset = (_target.position - transform.position).z;
    12.  
    13.  
    14.     private void LateUpdate()
    15.     {
    16.         // transform.position =
    17.         //     Vector3.SmoothDamp(transform.position, _target.position - Vector3.forward * _zOffset,
    18.         //         ref _moveLerp, Time.deltaTime * _currentSpeed * _lerpTime);
    19.  
    20.         transform.position = Vector3.Lerp(transform.position, _target.position - Vector3.forward * _zOffset,
    21.             Time.deltaTime * _currentSpeed * _lerpTime);
    22.     }
    23. }
     
  2. demozbox

    demozbox

    Joined:
    Nov 26, 2014
    Posts:
    83
    Enabling "isKinematic" fixes the issue but I need body to be non kinematic as I need to use AddForce to emulate gravity and impact.
     
  3. Edy

    Edy

    Joined:
    Jun 3, 2010
    Posts:
    2,428
    In non-kinematic bodies you should use AddForce only. Using MovePosition means overriding the results of the physics solver, so both you and the solver are giving contradictory positions to the rigidbody, hence the jittering.

    Note that you may use AddForce with ForceMode.VelocityChange to impose a velocity to the kinematic body. For example:
    Code (CSharp):
    1. rigidbody.AddForce (expectedVelocity - rigidbody.velocity, ForceMode.VelocityChange)
    You could compute expectedVelocity starting with the current velocity (rigidbody.velocity) and considering your imposed direction and speed (i.e. "speed should be n in the x axis" - that's plain vector math).
     
    Last edited: Aug 17, 2021
    demozbox likes this.
  4. fddfh

    fddfh

    Joined:
    Oct 28, 2016
    Posts:
    1
    Eh? You meant in non-kinematic bodies?
     
  5. demozbox

    demozbox

    Joined:
    Nov 26, 2014
    Posts:
    83
    non-kinematic => add force
    kinematic => MovePosition and also enable interpolation for smooth movement.
     
  6. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    10,529
    And just to be extra clear, "Non Kinematic" means "Dynamic". Three body types, Dynamic, Kinematic and Static. :)
     
  7. entirelydoomed

    entirelydoomed

    Joined:
    Mar 26, 2021
    Posts:
    67
    I think you shouldn't use MovePosition if you want your player to be affected by other forces, because as i understand, move position is almost the same as just manually setting your rigidbody position every frame. Maybe that's what is causing the jitter, because some force is applied but your moveposition sets the position back and so on
     
    Edy likes this.
  8. Edy

    Edy

    Joined:
    Jun 3, 2010
    Posts:
    2,428
    Yes, sorry. Fixed.