Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Rotation script if clause not working

Discussion in 'Scripting' started by matias-e, Mar 4, 2015.

  1. matias-e

    matias-e

    Joined:
    Sep 29, 2014
    Posts:
    106
    I created a script that reverses rotation when the angle of a game object is smaller than 90 and not reverse the rotation when the game object is over 90 degrees Z, but it isn't working for some reason. Here's the bit from the code:

    Code (CSharp):
    1. if (Input.GetKey(KeyCode.Mouse0) && !Input.GetKey(KeyCode.Mouse1))
    2.         {
    3.             Debug.Log("stuff");
    4.  
    5.             if(box1.transform.rotation.z < 90)
    6.             {
    7.                 Debug.Log ("Smaller");
    8.                 box1.transform.Rotate(spinnerX * -0.25f, 0.0f, spinnerY * 0.5f);
    9.                 box2.transform.Rotate(spinnerX * -0.25f, 0.0f, spinnerY * 0.5f);
    10.                 box3.transform.Rotate(spinnerX * -0.25f, 0.0f, spinnerY * 0.5f);
    11.                 box4.transform.Rotate(spinnerX * -0.25f, 0.0f, spinnerY * 0.5f);
    12.  
    13.             }
    14.        
    15.             else
    16.             {
    17.                 Debug.Log("bigger");
    18.                 box1.transform.Rotate(spinnerX * -0.25f, 0.0f, spinnerY * 0.5f);
    19.                 box2.transform.Rotate(spinnerX * -0.25f, 0.0f, spinnerY * 0.5f);
    20.                 box3.transform.Rotate(spinnerX * -0.25f, 0.0f, spinnerY * 0.5f);
    21.                 box4.transform.Rotate(spinnerX * -0.25f, 0.0f, spinnerY * 0.5f);
    22.  
    23.             }
    24.  
    25.         }
    26.  

    The code goes straight to the first if, but never returns to the else statement. Is the syntax totally wrong here?
     
    Last edited: Mar 4, 2015
  2. BenZed

    BenZed

    Joined:
    May 29, 2014
    Posts:
    524
    First of all, use Input.GetMouseDown()

    Second, the code pasted in your else statement is identical to the code pasted in the if statement, save for the Debug.Log("bigger").

    Are you very sure that "bigger" isn't showing up in your console?
     
  3. matias-e

    matias-e

    Joined:
    Sep 29, 2014
    Posts:
    106
    Ok, I changed the inputs to GetMouseButton. Sorry for the weird mistake in the code bit. I was changing the numbers from side to side trying to figure it out, edited the post and pasted the code in a pretty awkward phase. My bad.

    I'm sure that 'bigger' is not showing up in my console. I commented out the other two Debug.Log bits and nothing is showing up. Looking at the object being rotated in play mode it is going over 90 degrees Z, to well above 100.

    To test it out, I changed the code around a bit and made the if rotation < 90 to if rotation > 90. Now the code jumps right into the else part. Now it prints 'stuff' + 'bigger' only, still not responding to the change in angle. I guess I've done something wrong when it comes to tracking the actual angle of the box I'm rotating.