Fast camera rotation problem

Hi everyone,

I’ve searched quite a bit to find a solution to the problem that I have.
Now the problem is, as I want to rotate the camerathere isn’t a problem.
But the problem occurs when I move the mouse to fast. Then my camera makes a weird movement.
Is there someone that has a solution to this? You’re most welcome.

Here is the code that I use on my camera:

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

public class Camera_Controller_1 : MonoBehaviour
{
    public static Camera_Controller_1 instance;

    public Transform cameratransform;

    //KEYS
    public KeyCode moveForward = KeyCode.Z;
    public KeyCode moveBackward = KeyCode.S;
    public KeyCode moveLeft = KeyCode.Q;
    public KeyCode moveRight = KeyCode.D;

    public KeyCode rotateLeft = KeyCode.A;
    public KeyCode rotateRight = KeyCode.E;
    //ALT KEYS
    public KeyCode moveForwardALT = KeyCode.UpArrow;
    public KeyCode moveBackwardALT = KeyCode.DownArrow;
    public KeyCode moveLeftALT = KeyCode.LeftArrow;
    public KeyCode moveRightALT = KeyCode.RightArrow;

    //FLOATS
    public float normalSpeed;
    public float fastSpeed;
    public float movementSpeed;
    public float movementTime;

    public float rotationAmount;

    Vector3 rotateStartPosition;
    Vector3 rotateCurrentPosition;
    Quaternion newRotation;

    private void Awake()
    {
        instance = this;
    }

    // Start is called before the first frame update
    void Start()
    {
        newPosition = transform.position;
        newRotation = transform.rotation;
    }

    // Update is called once per frame
    void Update()
    {
        HandleMovementInput();
        HandleMouseInput();
    }

    void HandleMouseInput()
    {
        if (Input.GetMouseButtonDown(2))
            rotateStartPosition = Input.mousePosition;

        if (Input.GetMouseButton(2))
        {
            rotateCurrentPosition = Input.mousePosition;
            Vector3 difference = rotateStartPosition - rotateCurrentPosition;
            rotateStartPosition = rotateCurrentPosition;
            newRotation *= Quaternion.Euler(Vector3.up * (-difference.x / 5f));
        }
    }

    void HandleMovementInput()
    {
        if (Input.GetKey(KeyCode.LeftShift))
            movementSpeed = fastSpeed;
        else
            movementSpeed = normalSpeed;

        if (Input.GetKey(moveForward) || Input.GetKey(moveForwardALT))
            newPosition += (transform.forward * movementSpeed);

        if (Input.GetKey(moveBackward) || Input.GetKey(moveBackwardALT))
            newPosition += (transform.forward * -movementSpeed);

        if (Input.GetKey(moveLeft) || Input.GetKey(moveLeftALT))
            newPosition += (transform.right * -movementSpeed);

        if (Input.GetKey(moveRight) || Input.GetKey(moveRightALT))
            newPosition += (transform.right * movementSpeed);

        if (Input.GetKey(rotateLeft))
            newRotation *= Quaternion.Euler(Vector3.up * rotationAmount);

        if (Input.GetKey(rotateRight))
            newRotation *= Quaternion.Euler(Vector3.up * -rotationAmount);

        transform.position = Vector3.Lerp(transform.position, newPosition, Time.deltaTime * movementTime);
        transform.rotation = Quaternion.Lerp(transform.rotation, newRotation, Time.deltaTime * movementTime);
    }
}

What does “a weird movement” mean?

A common problem with fast sudden rotations (or movements in general) in movies or on computers is that you only have a limited framerate, and short sharp movements destroy the illusion of continuous movement. A fast but continuous rotation can also look like a different speed than it actually is–and can even look like it’s spinning backwards–due to the wagon-wheel effect.

1 Like

Thanks for replying.
I have a video about the problem:
https://drive.google.com/open?id=1jXsIMKFbrCo9C-8kXJBX17resNW9FSXV
Hope this gives you a better understanding about the problem that I have right know.
I still haven’t found a solution for it.

Quaternions will always represent the shortest possible rotation to reach the target. When you rotate too fast, you go over 180 degrees in a frame, and your lerp immediately starts rotating the opposite way, as that is now the shorter path to travel.

Easiest fix is to limit the max rotation amount to 179 degrees in a frame. Nobody needs to rotate that fast in normal gameplay.

There are things you can do to actually fix this, but they are more complicated than a one line max function and not worth the trouble.

1 Like

Thanks for the answer, I’m not a math expert, but it seems one value (now known as rotationLimiter) has to do something with the rotation speed.
I’ve made it a higher value and now the problem is gone. Thanks again!

if (Input.GetMouseButton(2))
        {
            rotateCurrentPosition = Input.mousePosition;
            Vector3 difference = rotateStartPosition - rotateCurrentPosition;
            rotateStartPosition = rotateCurrentPosition;
            newRotation *= Quaternion.Euler(Vector3.up * (-difference.x / rotationLimiter));
        }