At my Unity project I made character controller based on Rigidbody and new unity input system. The problem is not correct traectory of movement of my character, "shaky" movements, sometimes whole game freezing and lagging, and finally after another freeze player can just fall through the floor or fly away. This is te functions to control my character:

         private void Move() { 
            if(!_hasAnimator) { return;}

            float targetSpeed = _inputManager.Run ? _runSpeed : _walkSpeed;//just choosing speed if player press Shift
            if(_inputManager.Move == Vector2.zero) targetSpeed = 0;//imput manager is just a class that get the data from input system, Move variable is Vector2 value and it's based on WASD input data

            _currentVelocity.x = Mathf.Lerp(_currentVelocity.x, _inputManager.Move.x * targetSpeed, AnimBlendSpeed * Time.fixedDeltaTime);//_currentVelocity is Vector2 variable
            _currentVelocity.y = Mathf.Lerp(_currentVelocity.y, _inputManager.Move.y * targetSpeed, AnimBlendSpeed * Time.fixedDeltaTime);


            if(Mathf.Abs(_currentVelocity.x) < 0.01) _currentVelocity.x = 0;
            if(Mathf.Abs(_currentVelocity.y) < 0.01) _currentVelocity.y = 0;


            var xVelDifference = _currentVelocity.x - _playerRigidbody.velocity.x;//_playerRigidbody is Rigidbody component from the player
            var zVelDifference = _currentVelocity.y - _playerRigidbody.velocity.z;

            _playerRigidbody.AddForce(transform.TransformVector(new Vector3(xVelDifference, 0, zVelDifference)), ForceMode.VelocityChange);
        
            _animator.SetFloat(_xVelHash, _currentVelocity.x);//Just animation system, not about movement the Rigidbody 
            _animator.SetFloat(_yVelHash, _currentVelocity.y); 
        }



        private void CameraMovements() { 
             if(!_hasAnimator) { return;}

            var Mouse_X = _inputManager.Look.x;//info about mouse from new imput system
            var Mouse_Y = _inputManager.Look.y;
            Camera.position = CameraRoot.position;

            _xRotation -= Mouse_Y * MouseSensitivity * Time.smoothDeltaTime;
            _xRotation = Mathf.Clamp(_xRotation, UpperLimit, BottomLimit);

            Camera.localRotation = Quaternion.Euler(_xRotation, 0, 0);//Vertical movement of a camera
            _playerRigidbody.MoveRotation(_playerRigidbody.rotation * Quaternion.Euler(0, Mouse_X * MouseSensitivity * Time.smoothDeltaTime, 0));//whole body rotation, the camera attached to body so it rotates properly
        }

The function Move() is called on FixedUpdate(), the function CameraMovements() is called on LateUpdate().

The interpolation of Rigidbody is on.

that's how problem looks like - even on gif i can see the shaking and jiggling, and at the end - i use another function to make a slide and fall through the floor (like parkour move, that's not infuence the collider or rigidbody, it's just applying the animation on the animator. This problem also appears sometimes if i landing after jump, but not constantly)

I suppose the problem can be at the physics properties, but i'm not sure... Help!

Tried to change the project physics properties, but it didn't worked.

1

There are 1 best solutions below

0
CrackerBone On

Found the mistake, it was all about wrong velocity value. Changed Move() function:

private void Move() { 
        if(!_hasAnimator) { return;}

        float targetSpeed = _inputManager.Run ? _runSpeed : _walkSpeed;
        if(_inputManager.Move == Vector2.zero) targetSpeed = 0;

        if(Mathf.Abs(_currentVelocity.x) < 0.01) _currentVelocity.x = 0;
        if(Mathf.Abs(_currentVelocity.y) < 0.01) _currentVelocity.y = 0;

        _currentVelocity.x = Mathf.Lerp(_currentVelocity.x ,_inputManager.Move.x * targetSpeed, AnimBlendSpeed * Time.fixedDeltaTime);
        _currentVelocity.y = Mathf.Lerp(_currentVelocity.y ,_inputManager.Move.y * targetSpeed, AnimBlendSpeed * Time.fixedDeltaTime);

        _playerRigidbody.velocity = transform.TransformVector(new Vector3(_currentVelocity.x, _playerRigidbody.velocity.y, _currentVelocity.y));


        _animator.SetFloat(_xVelHash , _currentVelocity.x);
        _animator.SetFloat(_yVelHash, _currentVelocity.y); 
    }

I recommend to not use AddForce(V3, ForceMode.VelocityChange), if you do not sure about the final result of your velocity changes.