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

Question Clamping with SmoothDamp

Discussion in 'Scripting' started by redstoner8989, Aug 5, 2023.

  1. redstoner8989

    redstoner8989

    Joined:
    Jan 15, 2023
    Posts:
    7
    Why is my vertical head movement on the X axis getting successfully clamped to the maximum (80 degrees when looking down) but doesn't get clamped and gets buggy when looking up?
    When looking up, the camera jitters around 89 and 91 degrees. It puts me in that locked state once I move the camera where the clamps ends, -80 degrees.

    I'm guessing the problem has to do with SmoothDamp (that I use to smoothen the rotation), but I can't think of a fix.

    To clarify, it's a first-person controller camera script. You're supposed to be able to rotate the camera from -80 to 80 degrees up and down.

    Code (CSharp):
    1.     void Update()
    2.     {
    3.  
    4.         float mouseX = Input.GetAxis("Mouse X") * sensitivity;
    5.         float mouseY = Input.GetAxis("Mouse Y") * sensitivity;
    6.  
    7.  
    8.         Vector3 endRotationCam = new Vector3(-mouseY, 0, 0) + transform.localEulerAngles;
    9.         Vector3 endRotationBody = new Vector3(0, mouseX, 0) + charObject.localEulerAngles;
    10.  
    11.         Vector3 clampedRotationCam = new Vector3(Mathf.Clamp(endRotationCam.x, -80f, 80f), 0, 0);
    12.  
    13.         Debug.Log(clampedRotationCam);
    14.  
    15.        
    16.  
    17.         transform.localRotation = Quaternion.Euler(Vector3.SmoothDamp(transform.localEulerAngles, clampedRotationCam, ref currentVelocity, smoothTime));
    18.         charObject.localEulerAngles = Vector3.SmoothDamp(charObject.transform.localEulerAngles, endRotationBody, ref currentVelocity2, smoothTime);
    19.  
    20.  
    21.     }
     
  2. wideeyenow_unity

    wideeyenow_unity

    Joined:
    Oct 7, 2020
    Posts:
    728
    Anytime I had an issue with jittering when playing around with rotations, is 2 separate things were trying do the same thing. Most likely because I forgot about the other thing, lol, which happens a lot in my case.

    So without knowing if there's any other line of code somewhere else, that would be modifying:
    Code (CSharp):
    1. if (rotation.x > 90 - 1 && rotation.x < 90 + 1)
    2.    rotation.x = 90;
    Could be the culprit. As I've done things just like that, when the rotation winds up stopping half a degree off, or something similar.
     
    redstoner8989 likes this.
  3. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,563
  4. redstoner8989

    redstoner8989

    Joined:
    Jan 15, 2023
    Posts:
    7
    Hahaha happens to me all the time too, but since this is just a first person movement demo I don't have other scripts affecting the rotation
     
    wideeyenow_unity likes this.
  5. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,563
  6. zulo3d

    zulo3d

    Joined:
    Feb 18, 2023
    Posts:
    510
    transform.localEulerAngles is in the range of 0 to 360. Your script is assuming it's in the range of -180 to 180. Even if you fix the first part of your script by converting the angle to -180 to 180 you're still going to have problems because you're then trying to SmoothDamp a vector that's in the range of 0 to 360 to a vector that's in the range of -180 to 180.

    I guess you're trying to create a mouse look script with smoothing. Have a look around for one that's already made:

    https://forum.unity.com/threads/a-free-simple-smooth-mouselook.73117/
     
    redstoner8989 likes this.
  7. redstoner8989

    redstoner8989

    Joined:
    Jan 15, 2023
    Posts:
    7
    I have just read the article and your posts, but since I'm quite new to Unity and suck at rotations I'm struggling to understand how to apply quaternions in this case. I will try again when I'm home in a few hours.