Search Unity

Limiting Rotation

Discussion in 'Scripting' started by Soapstar88, Oct 10, 2013.

  1. Soapstar88

    Soapstar88

    Joined:
    Sep 20, 2013
    Posts:
    10
    Hi,

    I'm having a very simple problem when rotating an object. I have a physical control switch that I want to be turned 180 degrees on a key press, then back again anti-clockwise 180 degrees on another key press. The problem im having is limiting the rotation.

    Using the following code the object will rotate to 180 degrees and stop, but when rotating back to the start position it wont stop, it continues to rotate in the anti-clockwise direction.

    Code (csharp):
    1.  
    2. static var thePos = new Array();//hold the position of the control
    3. thePos[0] = 0;
    4.  
    5. static var turnKnobDirection1:boolean = false;
    6. static var turnKnobDirection2:boolean = false;
    7.  
    8. function Update (){
    9.  
    10.     if(turnKnobDirection1 == true){
    11.    
    12.         if(transform.localEulerAngles.z < 180){
    13.        
    14.             transform.Rotate(0,0,100 * Time.deltaTime);//TURN CLOCKWISE
    15.            
    16.         }
    17.        
    18.     }
    19.    
    20.     if(turnKnobDirection2 == true){
    21.    
    22.         if(transform.localEulerAngles.z > 0){//WONT STOP AT 0 DEGREES
    23.        
    24.             transform.Rotate(0,0,-100 * Time.deltaTime);//TURN ANTI-CLOCKWISE
    25.            
    26.         }
    27.        
    28.     }
    29.  
    30.  
    31.     if(Input.GetKeyDown(KeyCode.D)){
    32.    
    33.    
    34.         if(thePos[0] == 0){//TURN CLOCKWISE
    35.        
    36.             turnKnobDirection1 = true;
    37.             turnKnobDirection2 = false;
    38.             thePos[0] = 1;
    39.            
    40.            
    41.         }else if(thePos[0] == 1){//TURN ANTI-CLOCKWISE
    42.            
    43.             turnKnobDirection1 = false;
    44.             turnKnobDirection2 = true;
    45.             thePos[0] = 0;
    46.  
    47.         }
    48.        
    49.        
    50.     }
    51. }
    52.  
    It seems to miss the start rotation of 0 degrees and then continue to rotate back from 360 degrees and round again to 0 degrees.

    Can anyone help me please? Its driving me crazy!


    Thanks
     
  2. fire7side

    fire7side

    Joined:
    Oct 15, 2012
    Posts:
    1,819
    I'm not sure there is a 0 degrees, because you can't have both 360 and 0, so it might use 360, perhaps try >1, or .1, or .001.
     
  3. Marsupilami

    Marsupilami

    Joined:
    Sep 22, 2013
    Posts:
    21
    That's because Euler angles usually never go negative and you are checking for greater than zero. In 99.9% of the time you check EulerAngle it will be between 0.000001 and 359.999999

    So you only really need to check in the angle is less than 180 in both directions.

    Code (csharp):
    1.  
    2. static var thePos = new Array();//hold the position of the control
    3. thePos[0] = 0;
    4.  
    5. static var turnKnobDirection1:boolean = false;
    6. static var turnKnobDirection2:boolean = false;
    7.  
    8. function Update (){
    9.     if(transform.localEulerAngles.z < 180){
    10.         if(turnKnobDirection1 == true){
    11.             transform.Rotate(0,0,100 * Time.deltaTime);//TURN CLOCKWISE
    12.         }
    13.         if(turnKnobDirection2 == true){    
    14.             transform.Rotate(0,0,-100 * Time.deltaTime);//TURN ANTI-CLOCKWISE
    15.         }
    16.     }
    17.  
    18.     if(Input.GetKeyDown(KeyCode.D)){
    19.         if(thePos[0] == 0){//TURN CLOCKWISE
    20.             turnKnobDirection1 = true;
    21.             turnKnobDirection2 = false;
    22.             thePos[0] = 1;
    23.         }else if(thePos[0] == 1){//TURN ANTI-CLOCKWISE
    24.             turnKnobDirection1 = false;
    25.             turnKnobDirection2 = true;
    26.             thePos[0] = 0;
    27.         }
    28.     }
    29. }
    30.  
     
    Last edited: Oct 11, 2013