Rigidbody Based Third Person Controller Stutter Problem
Hello, I’m trying to make a Rigidbody based third person player controller, I want it to be smooth but I have a stutter problem. When player jumping and landing character start stutter but if player only moves on ground stutter is almost invisible. And if player rotates camera character starts stuttering.
Camera Controls which works in LateUpdate
private void UpdateCamera(){
// position
_camParent.position = Vector3.Lerp(_camParent.position, _camTarget.position, _followSmoothTime * Time.deltaTime);
// rotation
_x += _input.look.x * _sensitivity;
_y -= _input.look.y * _sensitivity;
_x = ClampAngle(_x, float.MinValue, float.MaxValue);
_y = ClampAngle(_y, _bottomClamp, _topClamp);
Quaternion wantedRot = Quaternion.Euler(_y, _x, 0.0f);
_camParent.rotation = Quaternion.Lerp(_camParent.rotation, wantedRot, _rotateSmoothTime * Time.deltaTime);
}
Movement and Jump Functions which works in FixedUpdate
private void Move(){
float targetSpeed = 0.0f;
if(_input.move != Vector2.zero) targetSpeed = _input.sprint ? _sprintSpeed : _walkSpeed;
// start moving and stop moving speeds
if(_input.move != Vector2.zero) _speed = Mathf.Lerp(_speed, targetSpeed, _speedStartSmoothTime * Time.deltaTime);
else if(_input.move == Vector2.zero) _speed = Mathf.Lerp(_speed, targetSpeed, _speedStopSmoothTime * Time.deltaTime);
if (_input.move != Vector2.zero){
// rotation smooth time calculation
Vector3 vel = new Vector3(_rb.velocity.x, 0.0f, _rb.velocity.z);
if(vel.magnitude < _threshold) _rotationSmoothTime = _moveStartRotationSmoothTime;
else _rotationSmoothTime = Mathf.Lerp(_rotationSmoothTime, _movingRotationSmoothTime, 5.0f * Time.deltaTime);
// calculate rotation
_targetRotation = Mathf.Atan2(_input.move.x, _input.move.y) * Mathf.Rad2Deg + _cam.transform.eulerAngles.y;
_rotation = Mathf.SmoothDampAngle(transform.eulerAngles.y, _targetRotation, ref _rotationVel, _rotationSmoothTime);
// rotate player
transform.rotation = Quaternion.Euler(0.0f, _rotation, 0.0f);
}
else{
// decrease smooth time
_rotationSmoothTime = Mathf.Lerp(_rotationSmoothTime, _moveStartRotationSmoothTime, 5.0f * Time.deltaTime);
}
Vector3 moveDir = Vector3.Cross(transform.right, _hit.normal) * _speed;
// move
_rb.velocity = new Vector3(moveDir.x, _rb.velocity.y, moveDir.z);
}
private void Jump(){
if(!_isGrounded) {
_input.jump = false;
return;
}
if(_input.jump) _rb.AddForce(transform.up * _jumpForce, ForceMode.Impulse);
}
Comment
Your answer
Follow this Question
Related Questions
Character Leaning 0 Answers
Rigidbody movement: Changing the Y of transform.position makes my character stutter 0 Answers
Stuttering nav Mesh Agent movement when i move the character 0 Answers
Rigidbody movement relative to third person camera 0 Answers
Npc spawning outside the camera view and sliding down within the camera view 0 Answers