I’m writing a basic FPS controller and using the following for mouse look:
public float sensitivity = 1.0F;
public float smoothing = 1.0F;
private float minimumX = -80F;
private float maximumX = 80F;
float rotationX = 0F;
float rotationY = 0F;
float smoothedX = 0F;
float smoothedY = 0F;
void Update()
{
rotationX += Input.GetAxisRaw("Mouse Y") * sensitivity;
rotationY += Input.GetAxisRaw("Mouse X") * sensitivity;
rotationX = Mathf.Clamp(rotationX, minimumX, maximumX);
smoothedX = Mathf.Lerp(smoothedX, rotationX, 1.0F / smoothing);
smoothedY = Mathf.Lerp(smoothedY, rotationY, 1.0F / smoothing);
transform.localEulerAngles = new Vector3(-smoothedX, smoothedY, 0.0f);
}
I have been stuck for days trying to get my player to rotate in the camera direction so that they can walk forward based on where you look. I have tried many things and all of them result in either the player not turning, or it “working” but having the camera get choppy whenever I move AND rotate at the same time.
I am moving the player like this:
private void GetInput()
{
vInput = Input.GetAxis("Vertical");
hInput = Input.GetAxis("Horizontal");
jumpInput = Input.GetAxisRaw("Jump");
}
private void Run()
{
vel.z = vInput * speed;
vel.x = hInput * speed;
}
private void Update()
{
GetInput();
Run();
Jump();
rb.velocity = transform.TransformDirection(vel);
}
Can someone please explain to me how to properly do this setup and how to stop the camera jitter? Both the camera and the player are being rotated in Update() so I don’t think they should be out of sync in their movements but I must be misunderstanding something.