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

[Help] How do I clamp Quaternion rotation?

Discussion in 'Scripting' started by OldManTies, Oct 30, 2016.

  1. OldManTies

    OldManTies

    Joined:
    Aug 12, 2016
    Posts:
    5
    So I was trying to make an FPS character controller, but my camera can rotate 360 degrees upwards and downwards (which I don't want to happen).

    Here is my rotation script in C#, can someone please tell me how to clamp the rotation?

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class YaxisLook : MonoBehaviour {
    5.  
    6.     public float rotateVel = 100;
    7.     public float inputDelay = 0.1f;
    8.  
    9.     public float yMin;
    10.     public float yMax;
    11.     private float yRot;
    12.  
    13.     public float turnInput;
    14.  
    15.     private Quaternion targetRotation;
    16.  
    17.     // Use this for initialization
    18.     void Start () {
    19.         targetRotation = transform.rotation;
    20.         turnInput = 0;
    21.     }
    22.    
    23.     // Update is called once per frame
    24.     void Update () {
    25.         turnInput = Input.GetAxis("Mouse Y");
    26.  
    27.         if (Mathf.Abs(turnInput) > inputDelay)
    28.         {
    29.             targetRotation *= Quaternion.AngleAxis(rotateVel * -turnInput * Time.deltaTime, Vector3.right);
    30.         }
    31.         transform.rotation = targetRotation;
    32.     }
    33. }
    34.  
     
  2. takatok

    takatok

    Joined:
    Aug 18, 2016
    Posts:
    1,496
    We could naively just try to clamp the y values between some yMin and YMax, but the problem is when you rotate downwards your going from 0 to some value say 45. So thats fine. But when you rotate up to -45 Unity is turning that number in to 315. So we need to split up our clamp between up and down.

    Code (CSharp):
    1. // set this in degrees up like 45 or 60
    2. // the code below converts it
    3. public float maxUpAngle;
    4. // Same for down.  Should be max degrees down you can rotate
    5. public float maxDownAngle;
    6. private float actualUpAngle;
    7.  
    8. void Awake()
    9. {
    10.    actualUpAngle = 360-maxUpAngle;
    11. }
    12. void Update()
    13.    turnInput = Input.GetAxis("Mouse Y");
    14.    if (Math.Abs(turnInput ) > inputDelay)
    15.    {
    16.        targetRotation *= Quaternion.AngleAxis(rotateVel * -turnInput * Time.deltaTime, Vector3.right);
    17.        Vector3 eulerAngles =  targetRotation.eulerAngles;
    18.        if (eulerAngles.y < 180)
    19.             eulerAngles.y=Mathf.Clamp(eulerAngles.y,0,maxDownAngle);
    20.        else
    21.             eulerAngles.y = Mathf.Clamp(eulerAngles.y,actualUpangle,360);
    22.        targetRotation = Quaternion.Euler(eulerAngles);
    23.    }
    24.    transform.rotation = targetRotation;
    25. }
    Just to be clear i didn't copy all your code you posted, you still need the other stuff you have in there.
     
  3. BBrown4

    BBrown4

    Joined:
    Feb 13, 2013
    Posts:
    15
    This post is so old but thank you so much. I have been bangin' my head against my desk trying to remember how this worked for hours. Soon as I read your post it all came flooding back. Not to mention it took me hours to find your post as well. Thank you for the clear, easy explanation.