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

Simple Limitation of a rotation?

Discussion in 'Scripting' started by Asta_D, Aug 28, 2017.

  1. Asta_D

    Asta_D

    Joined:
    Oct 16, 2013
    Posts:
    15
    Hello

    First of all, i am not so familiar with scripting, becaus i come from the design perspektiv. What i try to do is a touch control to view an object. so far, it works by using the following script:
    Code (CSharp):
    1.  void Update()
    2.     {
    3.  
    4.         if (Input.touchCount == 1)
    5.         {
    6.             if (Input.touches.Length > 0 && Input.GetTouch(0).phase == TouchPhase.Moved)
    7.             {
    8.                 transform.rotation *= Quaternion.AngleAxis((Input.GetTouch(0).deltaPosition.y * rotSpeed), Vector3.left);
    9.                    
    10.                    
    11.                 }
    12.             }
    13.         }
    but now i want to limit the rotation to 90 degrees up and down. I tought about clamping the rotation, but i didn*t get, how to clamp that quoternian right.
    Thanks for any help.

    Greetings from Germany
    Asta
     
  2. BoogieD

    BoogieD

    Joined:
    Jun 8, 2016
    Posts:
    236
    Just use: transform.rotation.eulerAngles so you can use angle expression in degrees.
     
  3. StarManta

    StarManta

    Joined:
    Oct 23, 2006
    Posts:
    8,744
    Don't do this! Euler angles suck. It won't reliably give you a useful value for this kind of functionality. (It sometimes will, but it may unexpectedly break at some point, for the reasons detailed in the linked article.)

    I recommend having a Vector2 variable representing the amount you have dragged; you can safely use Mathf.Clamp on this value to limit your rotation. This value can then be applied to your transform's rotation using Quaternion.AngleAxis (make sure this is a one-way street though!)
    Code (csharp):
    1. Vector2 trackedRotation = Vector2.zero;
    2. void Update()
    3.     {
    4.  
    5.         if (Input.touchCount == 1)
    6.         {
    7.             if (Input.touches.Length > 0 && Input.GetTouch(0).phase == TouchPhase.Moved)
    8.             {
    9. trackedRotation.y += (Input.GetTouch(0).deltaPosition.y * rotSpeed);
    10. trackedRotation.y = Mathf.Clamp(trackedRotation.y, -90f, 90f);
    11.                 transform.rotation = Quaternion.AngleAxis(trackedRotation.y, Vector3.left);
    12.  }
    13. }
    14. }
    (Apologies for weird indentation, I typed it up in the input box and can't tab.)

    You really only need a float for your Y rotation, but I assume at some point you'll probably want to add in X as well, so a Vector2 will let you add that easily.
     
    Asta_D likes this.
  4. Asta_D

    Asta_D

    Joined:
    Oct 16, 2013
    Posts:
    15
    Thank you very much, StarManta! That works exactly as i want it!