Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Jittery Camera Rotation on RigidBody FPS Player

Discussion in 'Scripting' started by NotTimTam, Aug 24, 2020.

  1. NotTimTam

    NotTimTam

    Joined:
    Sep 24, 2015
    Posts:
    20
    I am creating a first-person game that uses a Rigidbody for the player rather than a PlayerController. I am trying to work on rotating the camera. I have a small issue when you keep the camera still, everything displays and looks fine. If you stand still and rotate the camera, it looks fine. But if you try to rotate the camera while moving, it is all jittery.

    Here is an example of the issue:


    Here are my scripts:

    PLAYER MOVEMENT:

    using System.Collections;
    using System.Collections.Generic;
    using System.Collections.Specialized;
    using System.Security.Cryptography;
    using System.Threading;
    using UnityEngine;

    public class CameraController : MonoBehaviour
    {
    public float mouseSensitivity = 100f;

    private Transform playerBody;

    float xRotation = 0f;

    // Start is called before the first frame update
    void Start()
    {
    playerBody = GetComponentInParent<Rigidbody>().transform;

    Cursor.lockState = CursorLockMode.Locked;
    Cursor.visible = false;
    }

    // Update is called once per frame
    void Update()
    {
    float mouseX = Input.GetAxis("Mouse X") * mouseSensitivity * Time.deltaTime;
    float mouseY = Input.GetAxis("Mouse Y") * mouseSensitivity * Time.deltaTime;

    xRotation -= mouseY;
    xRotation = Mathf.Clamp(xRotation, -90f, 90f);

    transform.localRotation = Quaternion.Euler(xRotation, 0f, 0f);

    playerBody.Rotate(Vector3.up * mouseX);
    }
    }



    CAMERA:

    using UnityEngine;
    using System.Collections;
    using System;

    public class PlayerController : MonoBehaviour
    {

    private Rigidbody rb;
    private CapsuleCollider m_Capsule;
    private Transform player;
    private Animator anim;

    public float speed = 10.0f;
    public float gravity = 10.0f;
    public float maxVelocityChange = 10.0f;
    public bool canJump = true;
    public float jumpHeight = 2.0f;
    private bool grounded = false;

    private Camera cam;
    private Transform camTransform;

    bool Grounded;
    void Start()
    {
    // Get RigidBody
    rb = GetComponent<Rigidbody>();
    rb.freezeRotation = true;
    rb.useGravity = false;

    // Get Camera
    cam = GetComponentInChildren<Camera>();
    camTransform = cam.GetComponent<Transform>();

    // Get Animator
    anim = GetComponentInChildren<Animator>();
    }

    void FixedUpdate()
    {
    Vector2 input = GetInput();

    Vector3 targetVelocity = cam.transform.forward * input.y + cam.transform.right * input.x;
    targetVelocity = Vector3.ProjectOnPlane(targetVelocity, Vector3.up).normalized;

    targetVelocity = targetVelocity * speed;

    // Apply a force that attempts to reach our target velocity
    Vector3 velocity = rb.velocity;
    Vector3 velocityChange = (targetVelocity - velocity);
    velocityChange.x = Mathf.Clamp(velocityChange.x, -maxVelocityChange, maxVelocityChange);
    velocityChange.z = Mathf.Clamp(velocityChange.z, -maxVelocityChange, maxVelocityChange);
    velocityChange.y = 0;
    rb.AddForce(velocityChange, ForceMode.VelocityChange);

    if (grounded)
    {
    // Jump
    if (canJump && Input.GetButton("Jump"))
    {
    rb.velocity = new Vector3(velocity.x, CalculateJumpVerticalSpeed(), velocity.z);
    }
    }

    // We apply gravity manually for more tuning control
    rb.AddForce(new Vector3(0, -gravity * rb.mass, 0));

    grounded = false;
    }


    private Vector2 GetInput()
    {
    Vector2 input = new Vector2
    {
    x = Input.GetAxis("Horizontal"),
    y = Input.GetAxis("Vertical")
    };
    return input;
    }

    void OnCollisionStay()
    {
    grounded = true;
    }
    float CalculateJumpVerticalSpeed()
    {
    // From the jump height and gravity we deduce the upwards speed
    // for the character to reach at the apex.
    return Mathf.Sqrt(2 * jumpHeight * gravity);
    }
    }
     
  2. Joe-Censored

    Joe-Censored

    Joined:
    Mar 26, 2013
    Posts:
    11,847
    Try moving the camera in LateUpdate instead of Update. Also, don't check for input in FixedUpdate as your input checks will be unreliable there.
     
    btomaek likes this.
  3. NotTimTam

    NotTimTam

    Joined:
    Sep 24, 2015
    Posts:
    20
    Thanks I'll try that...