- Home /
Question by
Nikewade · Oct 05, 2021 at 09:22 PM ·
camerafpsmovement script
When I move and turn the FPS camera at the same time it is super jittery,Jittery camera while moving
I am following a course and this is the code for movement / turning that I was given and it seems to be really jittery when I move from side to side and turn the camera at the same time. Does anyone know of a way to fix this?
public class Player : MonoBehaviour
{
[Header("Movement")]
public float moveSpeed; // movement speed in units per second
public float jumpForce; // force applied upwards
[Header("Camera")]
public float lookSensitivity; // mouse look sensitivity
public float maxLookX; // lowest down we can look
public float minLookX; // highest up we can look
private float rotX;
void Update ()
{
Move();
if(Input.GetButtonDown("Jump"))
TryJump();
CamLook();
}
// move horizontally based on movement inputs
void Move ()
{
// get the x and z inputs
float x = Input.GetAxis("Horizontal") * moveSpeed;
float z = Input.GetAxis("Vertical") * moveSpeed;
Vector3 dir = transform.right * x + transform.forward * z;
dir.y = rig.velocity.y;
// apply the velocity
rig.velocity = dir;
}
void CamLook ()
{
// get mouse inputs
float y = Input.GetAxis("Mouse X") * lookSensitivity;
rotX += Input.GetAxis("Mouse Y") * lookSensitivity;
// clamp the vertical rotation
rotX = Mathf.Clamp(rotX, minLookX, maxLookX);
// rotate the camera and player
cam.transform.localRotation = Quaternion.Euler(-rotX, 0, 0);
transform.eulerAngles += Vector3.up * y;
}
Comment