Buggy smoothing of camera

In my scene, there is a player Gameobject with a Character Controller and it contains a first person camera as child. To make the camera stutter less, I tried to use smoothing but there is some strange bug that makes the camera rotation bounce around.
Here is the script for the movement of the Character Controller, which is attached to the player GameObject:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Walking : MonoBehaviour
{
    public CharacterController playerController;
    public float moveSpeed;
    float hor;
    float ver;
    Vector3 direction;

    void getInput()
    {
        hor = Input.GetAxisRaw("Horizontal");
        ver = Input.GetAxisRaw("Vertical");
    }

    void Walk()
    {
        direction = (transform.right * hor + transform.forward * ver) * moveSpeed;
        playerController.Move(direction * Time.deltaTime);
    }

    void Update()
    {
        getInput();
        Walk();
    }

}

Here is the script for the first person camera, which is attached to the Camera GameObject. Remember, it’s the child of player GameObject:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class FirstPersonCamera : MonoBehaviour
{
    public Transform playerTransform;
    float mouseX;
    float mouseY;
    float rotX;
    float rotY;
    public float mouseRotationSpeed;
    float velocity = 0;

    private void Start()
    {
    }

    void GetInput()
    {
        mouseX = Input.GetAxisRaw("Mouse X");
        mouseY = Input.GetAxisRaw("Mouse Y");

        rotX += mouseY * mouseRotationSpeed;
        rotY += mouseX * mouseRotationSpeed;
        rotX = Mathf.Clamp(rotX, -90, 90);
    }

    void LockCursor()
    {
        if (Input.GetKey(KeyCode.Escape))
            Cursor.lockState = CursorLockMode.None;
        else
            Cursor.lockState = CursorLockMode.Locked;
    }

    void RotateY()
    {
        playerTransform.rotation = Quaternion.Euler(0, Mathf.SmoothDampAngle(playerTransform.eulerAngles.y, rotY, ref velocity, 0.02f), 0);
    }

    void RotateX()
    {
        transform.localRotation = Quaternion.Euler(Mathf.SmoothDampAngle(transform.localEulerAngles.x, -rotX, ref velocity, 0.02f), 0, 0);
    }

    void Update()
    {
        GetInput();
        LockCursor();
    }
    private void LateUpdate()
    {
        RotateY();
        RotateX();
    }
}

When I only run RotateY() or RotateX() in LateUpdate, it works fine. But as soon as I add both, the Camera starts to bug.
Thanks in advance!

Camera stuff is pretty tricky… it might be best to use Cinemachine from the Unity Package Manager.

If you want to debug what you have above, you must find a way to get the information you need in order to reason about what the problem is.

What is often happening in these cases is one of the following:

  • the code you think is executing is not actually executing at all
  • the code is executing far EARLIER or LATER than you think
  • the code is executing far LESS OFTEN than you think
  • the code is executing far MORE OFTEN than you think
  • the code is executing on another GameObject than you think it is

To help gain more insight into your problem, I recommend liberally sprinkling Debug.Log() statements through your code to display information in realtime.

Doing this should help you answer these types of questions:

  • is this code even running? which parts are running? how often does it run? what order does it run in?
  • what are the values of the variables involved? Are they initialized? Are the values reasonable?
  • are you meeting ALL the requirements to receive callbacks such as triggers / colliders (review the documentation)

Knowing this information will help you reason about the behavior you are seeing.

You can also put in Debug.Break() to pause the Editor when certain interesting pieces of code run, and then study the scene

You could also just display various important quantities in UI Text elements to watch them change as you play the game.

If you are running a mobile device you can also view the console output. Google for how on your particular mobile target.

Here’s an example of putting in a laser-focused Debug.Log() and how that can save you a TON of time wallowing around speculating what might be going wrong:

1 Like