I have a problem with my camera when the player rotate the camera shake
what I want to do is to make the camera orbit the player but when the player moves he move toward the direction of the camera. I am done with that but when I added the rotation the camera started to shake and I dont know were the problem is
from what I observed the shake happen when there is a sadden movement
public class PlayerController : MonoBehaviour
{
/// <summary>
/// store the transform of the camera
/// </summary>
[SerializeField]
private Transform _camera;
/// <summary>
/// store the transform of the camera pivot object
/// </summary>
[SerializeField]
private Transform _cameraPivotPoint;
/// <summary>
/// store the mouse input in the x axis
/// </summary>
private float _mouseX = 0;
/// <summary>
/// store the mouse input in the Y axis
/// </summary>
private float _mouseY = 0;
/// <summary>
/// the walk speed of the player
/// </summary>
[SerializeField]
private float _walkPlayerSpeed = 3f;
/// <summary>
/// get the input from the user
/// </summary>
private Vector3 _input;
/// <summary>
/// reference to the player controller component
/// </summary>
private CharacterController _controller;
// Start is called before the first frame update
void Start()
{
_controller = GetComponent<CharacterController>();
}
private void FixedUpdate()
{
}
// Update is called once per frame
void Update()
{
Vector3 lastPosition = transform.position;
//add the value of the mouse input in the x axis
_mouseX += Input.GetAxis("Mouse X") ;
//add the value of the mouse input in the Y axis
_mouseY += -Input.GetAxis("Mouse Y");
//rotate the camera around the player
_cameraPivotPoint.eulerAngles = new Vector3(_mouseY, _mouseX, 0);
//store the input from the player for movement
_input = new Vector3(Input.GetAxis("Horizontal"), 0, Input.GetAxis("Vertical"));
//Normalize the input from the player
_input.Normalize();
//get the camera vector in the z axis
Vector3 cameraForward = _camera.forward;
//get the camera vector in the x axis
Vector3 cameraRight = _camera.right;
//reset the y axis value
cameraForward.y = 0;
cameraRight.y = 0;
//normalize the values
cameraForward.Normalize();
cameraRight.Normalize();
//move the player
_controller.Move((cameraForward * _input.z + cameraRight * _input.x) * Time.deltaTime * _walkPlayerSpeed);
//rotate the player model in the direction of the camera
PlayerRotation(_input);
}
private void PlayerRotation(Vector3 input)
{
//store the target direction to rotate the player
Vector3 targetDirection = Vector3.zero;
//
targetDirection = _camera.forward * input.z;
targetDirection += _camera.right * input.x;
//Normalize the values
targetDirection.Normalize();
//reset the y axis
targetDirection.y = 0;
//
if (targetDirection == Vector3.zero)
{
targetDirection = transform.forward;
}
//calc the rotation the player should rotate to
Quaternion targetRotation = Quaternion.Slerp(transform.rotation, Quaternion.LookRotation(targetDirection), 10 * Time.deltaTime);
//rotate the player
transform.rotation = targetRotation ;
}
}