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. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Jerky rotation problem

Discussion in 'Scripting' started by jarden, Dec 13, 2013.

  1. jarden

    jarden

    Joined:
    Mar 29, 2011
    Posts:
    74
    Hi, I've tried a bunch of things but for some reason my code jerks my car when I move the mouse to fast. Here is my code:
    Code (csharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class dfsag : MonoBehaviour {
    5.     private float mouse;
    6.     private Quaternion quats;
    7.  
    8.     private float ClampAngle (float angle,float min,float max) {
    9.         if (angle < 0)
    10.             angle += 360f;
    11.         if (angle > 360f)
    12.             angle -= 360f;
    13.         return angle;
    14.     }
    15.     void Update () {
    16.         mouse += Input.GetAxisRaw("Mouse X")* 995 * Time.deltaTime;
    17.         ClampAngle(mouse,0, 360f);
    18.     }
    19.     void FixedUpdate () {
    20.         quats = Quaternion.FromToRotation(Vector3.up,Vector3.up)*Quaternion.Euler (0,mouse,0);
    21.         transform.rotation = Quaternion.Slerp(transform.rotation,quats,Time.deltaTime*7);
    22.     }
    23. }
    It does exactly what I want except when I move the mouse fast it has a large jerky pause as long as i keep moving it so fast. I've tried angleaxis, transform.Rotate, moving mouse code to fixed update, etc etc. I can't just clamp the mouse delta because then the sensitivity would be slow. Can anyone help please?
     
  2. TheShane

    TheShane

    Joined:
    May 26, 2013
    Posts:
    136
    Firstly, you can just use "Quaternion.Euler(Vector3.up)" instead of "Quaternion.FromToRotation(Vector3.up,Vector3.up)" which is doing a bit of unnecessary work.

    Your problem is that when you move the mouse too fast, the angle wraps around and the Slerp detects that it is faster to start interpolating in the other direction. Instead of a smooth interpolation, it stops, reverses, and you get jerkiness.

    Instead of clamping the value, and always rotating toward a fixed angle, you could try to always keep track of the accumulated mouse delta and then rotate the angle a certain percentage of this delta each update to "catch up" to the mouse. The lower the percentage, the smoother your camera will be (but too smooth and it will feel unresponsive).
     
  3. jarden

    jarden

    Joined:
    Mar 29, 2011
    Posts:
    74
    Cant I slerp/lerp all axis except the rotation around y somehow?
     
  4. jarden

    jarden

    Joined:
    Mar 29, 2011
    Posts:
    74
    I ended up clamping the max angle per fixed update (line 16) to 20. Thanks for the reply.