Rotating an object 90 degrees with a mouseclick

I’m trying to make a model rotate 90 degrees on a certain axis once it’s clicked. I wrote this code but I’m getting a lot of errors I can’t understand that are apparently syntax issues.

Assets/Scripts/Angeler.cs(28,29): error CS1525: Unexpected symbol case1', expecting }‘, case', or default:’
Assets/Scripts/Angeler.cs(34,45): error CS1525: Unexpected symbol while' Assets/Scripts/Angeler.cs(38,30): error CS1519: Unexpected symbol :’ in class, struct, or interface member declaration
Assets/Scripts/Angeler.cs(38,40): error CS1519: Unexpected symbol ==' in class, struct, or interface member declaration Assets/Scripts/Angeler.cs(39,55): error CS1519: Unexpected symbol <’ in class, struct, or interface member declaration
Assets/Scripts/Angeler.cs(41,57): error CS1519: Unexpected symbol (' in class, struct, or interface member declaration Assets/Scripts/Angeler.cs(41,77): error CS1519: Unexpected symbol ,’ in class, struct, or interface member declaration
Assets/Scripts/Angeler.cs(43,30): error CS8025: Parsing error

using UnityEngine;
using System.Collections;

public class Angeler : MonoBehaviour {
    private float anglex;
    private bool IsSpinning;
    // Use this for initialization
    void Start () {
        anglex = transform.eulerAngles.x; //anglex is the rotation value of x
        print(anglex);

    }

    void OnMouseDown()
    {
        if (transform.rotation.eulerAngles [0] % 90 == 0) { //so the player won't be able to click while the cube is spinning
            IsSpinning = true;
        } else {
            IsSpinning = false;
        }
    }

    void Update () {
        if (IsSpinning)
        {
            switch (anglex)
            {
            case1: anglex == 0
                    while (anglex < 90) //every click will rotate 90 degrees
                {
                    transform.Rotate(30 * Time.deltaTime, 0, 0);
                }
            case2: anglex == 90
                    while (anglex < 180)
                {
                    transform.Rotate(30 * Time.deltaTime, 0, 0);
                }
            case3: anglex == 180
                    while (anglex < 270)
                {
                    transform.Rotate(30 * Time.deltaTime, 0, 0);
                }
            case4: anglex == 270
                    while (anglex < 360)
                {
                    transform.Rotate(30 * Time.deltaTime, 0, 0);
                }
            }
   
}
        if (anglex == 360)
        {
            anglex = 0; // restarting the sequence after the model completely spins 360.
        }
}

}

The case statements are wrong. It should be along the lines of:

switch (anglex)
{
   case 90:
   //do something here
   break;
   case 180:
   //do something else
   break;
}

have a look at:

1 Like

https://unity3d.com/learn/tutorials/modules/beginner/scripting/switch

1 Like

Problem is that “switch” does not accept floats :-/

I tried the various ways of converting floats to int but no success. Any ideas? Or should I redo the code and avoid switch?

to be honest, i think setting up a coroutine that will rotate the gameobject by 90 degrees from the initial starting rotation would be a better approach, it then wouldn’t care what the starting rotation was and would not need to check each case…

Well, maybe, but my code should work right? I changed the “switch” to if, if else statements…and every time I click on my object Unity freezes and I have to Clt+Alt+Del to end process. Why would it freeze?

using UnityEngine;
using System.Collections;

public class Angeler : MonoBehaviour {
    private float anglex;
    private bool IsSpinning;
    // Use this for initialization
    void Start () {
        anglex = transform.eulerAngles.x; //anglex is the rotation value of x
        print(anglex);

    }

    void OnMouseDown()
    {
        if (transform.rotation.eulerAngles [0] % 90 == 0) { //so the player won't be able to click while the cube is spinning
            IsSpinning = true;
        } else {
            IsSpinning = false;
        }
    }

    void Update () {
        if (IsSpinning)
        {
            if (anglex == 0)
            {
                while (anglex < 90) //every click will rotate 90 degrees
                {
                    transform.Rotate(30 * Time.deltaTime, 0, 0);
                }
            }
            else if (anglex == 90)
            {
                while (anglex < 180)
                {
                    transform.Rotate(30 * Time.deltaTime, 0, 0);
                }
            }
            else if (anglex == 180)
            {
            while (anglex < 270)
                {
                    transform.Rotate(30 * Time.deltaTime, 0, 0);
                }
            }
            else if (anglex == 270)
            {
            while (anglex < 360)
                {
                    transform.Rotate(30 * Time.deltaTime, 0, 0);
                }
            }
            }
   

        if (anglex == 360)
        {
            anglex = 0; // restarting the sequence after the model completely spins 360.
        }
}

}

you never update angleX… so while(anglex < xx) will infinite loop.

Your code is running within Update() you don’t need to use while’s

OK, noted. Now it almost works. It rotates 90 degrees when I click, but then I strangely have to click it 10 times to make it spin again and it rotates BACKWARDS until it reaches 270 degrees. WTF?

I want it to rotates 90 degrees positively every click.

using UnityEngine;
using System.Collections;

public class Angeler : MonoBehaviour {
    private float anglex;
    private bool IsSpinning;
    // Use this for initialization
    void Start () {
        anglex = transform.eulerAngles.x; //anglex is the rotation value of x
        print(anglex);
        IsSpinning = true;
       
       
    }
   
    void OnMouseDown()
    {
        anglex++;
        Debug.Log ("YO!");
        IsSpinning = true;
       
    }
   
   
    void Update () {
        anglex = transform.eulerAngles.x; //anglex is the rotation value of x
        if (IsSpinning == true) {
            if (anglex < 90) {
                transform.Rotate (30 * Time.deltaTime, 0, 0);
            } else if (anglex <= 180 && anglex >= 90) {
                transform.Rotate (30 * Time.deltaTime, 0, 0);
               
            } else if (anglex <= 270 && anglex >= 180) {
               
                transform.Rotate (30 * Time.deltaTime, 0, 0);
               
            } else if (anglex <= 360 && anglex >= 270) {
               
                transform.Rotate (30 * Time.deltaTime, 0, 0);
            }
           
            if (anglex == 0) {
                IsSpinning = false;
            } else if (anglex == 90) {
                IsSpinning = false;
            } else if (anglex == 180){
                IsSpinning = false;
            }
        } else if (anglex == 270) {
            IsSpinning = false;
        }
        else if (anglex == 360)
        {
            anglex = 0; // restarting the sequence after the model completely spins 360.
            IsSpinning = false;
        }
        Debug.Log (anglex);
        Debug.Log (IsSpinning);
    }
   
}

You’re making this way too hard on yourself mate. Use Mathf.MoveTowards() and simplify that big mess you have:

public class Angeler : MonoBehaviour
{
    private float currentRoation;
    private float targetRotation;

    void Start()
    {
        currentRotation = transform.eulerAngles.x;
        targetRotation = transform.eulerAngles.x;
    }

    void OnMouseDown()
    {
        if(currentRotation == targetRotation)
        {
            targetRotation += 90;
        }
    }

    void Update()
    {
        currentRotation = Mathf.MoveTowards(currentRotation, targetRotation, 30 * Time.deltaTime);
        transform.rotation = Quaternion.Euler(currentRotation, transform.eulerAngles.y, transform.eulerAngles.z);
    }
}

First off, thank you, but your code is also buggy. The cube jitters and glitches while it spins the second time and it strangely double-rotates after a single clicks. Going from 0 to 90 back to 0 and back to 90 then it stops, then if you click on it again it starts glitching.

I realize that my code is not the right way to do things. But theoretically, it should work right? Why doesn’t it work?

@GroZZleR he’s right, it goes a little glitchy once it’s past 90, current rotation/target rotation look fine but it looks like the euler <> quaternion conversion is flipping the y/z by 180.

@Valda_Vaux

if (anglex < 90) {
    transform.Rotate (30 * Time.deltaTime, 0, 0);
} else if (anglex <= 180 && anglex >= 90) {
    transform.Rotate (30 * Time.deltaTime, 0, 0);
   
} else if (anglex <= 270 && anglex >= 180) {
   
    transform.Rotate (30 * Time.deltaTime, 0, 0);
   
} else if (anglex <= 360 && anglex >= 270) {
   
    transform.Rotate (30 * Time.deltaTime, 0, 0);
}

this section is completely redundant… all of the if conditions overlap and the action to be taken is the same. Need to rethink the logic here.

if(anglex ==0){...

anglex is a float, it’s very unlikely it’s going to be exactly 0, rather 0.00000001 etc.

In general I’d agree with Groz and say it looks over-engineered.

I understand that and that’s why I changed the code to be

if (anglex > 0 && anglex < 1)
if (anglex > 89 && anglex < 91)


still the same thing happens. I do not intend for this to be the code for my game, I just want to know “why” it’s not working. I treat it as a learning experience.

PS I’m not a “he” I’m a “she” :stuck_out_tongue:

My apologies for the bad code. Quaternion.RotateTowards works flawlessly from what I can see:

public class CubeRotator : MonoBehaviour
{
   private Quaternion currentRotation;
   private Quaternion targetRotation;

   void Start()
   {
     currentRotation = transform.rotation;
     targetRotation = transform.rotation;
   }

   void OnMouseDown()
   {
     if (currentRotation == targetRotation)
     {
       targetRotation *= Quaternion.Euler(90.0f, 0.0f, 0.0f);
     }
   }

   void Update()
   {
     currentRotation = Quaternion.RotateTowards(transform.rotation, targetRotation, 30.0f * Time.deltaTime);

     transform.rotation = currentRotation;
   }
}
1 Like

First off, thank you.

Secondly, I still don’t understand something… here are the start rotation positions:
X: 0 Y: 0 Z: 0

First Click
X: 90 Y: 0 Z: 0

Second Click
X: 0 Y: 180 Z: 180

Third Click:
X: 270 Y: 0 Z: 0

Forth Click:

X: 0 Y: 0 Z: 0

Why do the Y and Z change if target rotation only indicates 90 degrees on X rotational axis?