Search Unity

How to set the limit of rotation

Discussion in 'Scripting' started by leekinon, Jun 18, 2015.

  1. leekinon

    leekinon

    Joined:
    Dec 4, 2013
    Posts:
    21
    Here is my code for drag to rotate a object....

    Code (CSharp):
    1.  
    2. using UnityEngine;
    3. using System.Collections;
    4.  
    5.  
    6. public class Touch : MonoBehaviour {
    7.  
    8.     //save the position of the finger
    9.     private Vector3 touchX = new Vector3();
    10.     private Vector3 touchY = new Vector3();
    11.  
    12.  
    13.  
    14.  
    15.     private float rotatePosX;
    16.     private float rotatePosXb;
    17.     private float rotatePosY;
    18.     private float rotatePosYb;
    19.     private bool touchaftercheck;
    20.     private bool touchbegincheck;
    21.     private bool touchaftercheckY;
    22.     private bool touchbegincheckY;
    23.  
    24.  
    25.  
    26.  
    27.     // Update is called once per frame
    28.     void Update () {
    29.  
    30.  
    31.         rotatePosXb = touchX.x - Input.mousePosition.x;
    32.         rotatePosYb = touchY.y - Input.mousePosition.y;
    33.  
    34.  
    35.        transform.localEulerAngles=new Vector3(Mathf.Clamp(transform.localEulerAngles.x,-45,45),transform.localEulerAngles.y,transform.localEulerAngles.z);
    36.  
    37.  
    38.  
    39.         if (touchbegincheck == true&&touchX.x!=Input.mousePosition.x) {
    40.            transform.localEulerAngles += new Vector3 (0,rotatePosXb/10, 0);
    41.             touchX.x=Input.mousePosition.x;
    42.         }
    43.         if (touchbegincheckY == true&&touchY.y!=Input.mousePosition.y) {
    44.      
    45.             transform.Rotate(rotatePosYb/10,0, 0, Space.World);
    46.             touchY.y=Input.mousePosition.y;
    47.         }
    48.         if(touchbegincheck == false&&touchX.x!=Input.mousePosition.x&&touchaftercheck==true)
    49.         {
    50.            transform.localEulerAngles += new Vector3 (0,rotatePosX/10, 0);
    51.             rotatePosX*=0.9f;
    52.             if(rotatePosX<=0.1&&rotatePosX>=-0.1)
    53.                 touchaftercheck=false;
    54.  
    55.         }
    56.         if(touchbegincheckY == false&&touchY.y!=Input.mousePosition.y&&touchaftercheckY==true)
    57.         {
    58.  
    59. transform.Rotate(rotatePosY/10,0, 0, Space.World);
    60.             rotatePosY*=0.9f;
    61.             if(rotatePosY<=0.1&&rotatePosY>=-0.1)
    62.                 touchaftercheckY=false;
    63.         }
    64.         MouseInput();   // mouse check
    65.  
    66.     }
    67.  
    68.     void MouseInput()
    69.     {
    70.         if (Input.GetMouseButtonDown(0))
    71.         {
    72.             touchX.x=Input.mousePosition.x;
    73.             touchY.y=Input.mousePosition.y;
    74.             touchbegincheck = true;
    75.             touchbegincheckY = true;
    76.         }
    77.    
    78.         if(Input.GetMouseButtonUp(0))
    79.         {
    80.             rotatePosX=rotatePosXb;
    81.             rotatePosY=rotatePosYb;
    82.             touchbegincheck = false;
    83.             touchbegincheckY = false;
    84.             touchaftercheck=true;
    85.             touchaftercheckY=true;
    86.  
    87.  
    88.         }
    89.    
    90.     }
    91. }
    I already add
    Code (CSharp):
    1. transform.localEulerAngles=new Vector3(Mathf.Clamp(transform.localEulerAngles.x,-45,45),transform.localEulerAngles.y,transform.localEulerAngles.z);
    But it is not work smoothly.The transform.localEulerAngles.x just turn to more than the limit and go back to 45 in one frame
     
    Last edited: Jun 18, 2015
  2. LeftyRighty

    LeftyRighty

    Joined:
    Nov 2, 2012
    Posts:
    5,148
    put the clamp code at the end of your function, not at the beginning. At the moment you clamp at the beginning of the frame, rotate outside the clamp range then display, next frame, clamp back, rotate outside, display etc.
     
  3. leekinon

    leekinon

    Joined:
    Dec 4, 2013
    Posts:
    21
    OK, but how to put the function outside the range and get work?
     
  4. leekinon

    leekinon

    Joined:
    Dec 4, 2013
    Posts:
    21
    I change the program from
    Code (CSharp):
    1. transform.localEulerAngles=new Vector3(Mathf.Clamp(transform.localEulerAngles.x,-45,45),transform.localEulerAngles.y,transform.localEulerAngles.z);
    to
    Code (CSharp):
    1. if(transform.localEulerAngles.x>45&&transform.localEulerAngles.x<200)
    2.             transform.localEulerAngles=new Vector3(45,transform.localEulerAngles.y,transform.localEulerAngles.z);
    3.         if(transform.localEulerAngles.x<305&&transform.localEulerAngles.x>100)
    4.             transform.localEulerAngles=new Vector3(-45,transform.localEulerAngles.y,transform.localEulerAngles.z);
    It work...........but when I move x axis, the limit function is fail to work.
     
  5. GargerathSunman

    GargerathSunman

    Joined:
    May 1, 2008
    Posts:
    1,571
    Your core method is incorrect. Due to the fact that you can have the same rotation with different values of x, y, and z, limiting localEulerAngles doesn't effectively work.

    What I would recommend is have variables for those values, modifying those values as you wish, and then setting localEulerAngles = to those angles. This way you won't have gimbal lock and you won't have to deal with the eulerAngles jumps that are inherent to certain degree cutoffs.
     
  6. leekinon

    leekinon

    Joined:
    Dec 4, 2013
    Posts:
    21
    it will get some error rotation after move other axis if it is not rotate in space world.
     
  7. leekinon

    leekinon

    Joined:
    Dec 4, 2013
    Posts:
    21
    Can anyone help me?