# How do i make my code better? (switch statement)

Hi here is my code:

``````switch (rotationCount)
{
case 1:
Rotation(new Vector3(0, 0, 90));
direction = Vector3.right;
break;
case 2:
Rotation(new Vector3(0, 0, 180));
direction = Vector3.back;
break;
case 3:
Rotation(new Vector3(0, 0, 270));
direction = Vector3.left;
break;
case 4:
Rotation(new Vector3(0, 0, 360));
direction = Vector3.forward;
rotationCount = 0;
break;
}
``````

I have a object attached to my mouse cursor and i have a function that lets me rotate it in 4 dimensions. It will go through the cases and at the end of case 4 it will go back to 1.

The code works fine but i want to know if there is way to make this code smaller and more efficient. Right now it’s doing the same thing with just some numbers being changed around.

``````private void Rotation(Vector3 currRot)
{
transform.eulerAngles = new Vector3(currRot.x, currRot.y, currRot.z);
}
``````

Here is how i rotate my gameobject, i just use this function in my switch statement and change currRot. How can i use this Rotation function to able to shuffle through 4 vectors in less than one line?

The thing is i got a hint on how i could make this code more efficient but i forgot about it. It has “%” in it if that rings any bells. If you have a solution in less than 1 or 2 lines of code that uses this % or not i would be most grateful.

Thanks!

As I don’t know the oint of ‘direction’ I am just going to ignore it, I assume the line you were previously told about is probably:

``````Rotation(new Vector3(0, 0, 90*(rotationCount%4)));
``````

or

``````Rotation(new Vector3(0, 0, 90*rotationCount));
rotationCount = (rotationCount+1)%4;
``````

I would suggest the second option if this runs ‘forever’ else I guess you’re rotationCount variable might… eventually be greater than Int.MaxInteger!

the ‘%’ signs refers to the modulo function, which returns the remainder after division, so 4%4, has 0 remainder when 4/4 so answer is 0; and 5%4 is 5/4 = 4/4 + 1 so our answer is the remained = 1.

@vittu1994: I have not seen the reference you are speaking of. That said, I could give you some pointers :).

First, the “%” operator is the modulus operator, and it returns the remainder value from a division. The second argument cannot be 0, otherwise it should return a DivideByZeroException.

Second, I’m surprised it works! The Vector3 arguments are floats, and to have a literal assignment (like “floatVar = 90f;”) rather then a variable (like “floatVar = floatOtherVar;”) you should have to add an ‘f’ immediately after the value, independent of whether or not it you used a decimal… A small difference between MonoDevelop’s and VS’s float semantics, I presume?

One simplified possible solution could be like this:

``````if(floatVar.z % 90 == 0f)
{
if(floatVar.z / 4 == 90f) direction = Vector3.right;
if(floatVar.z / 4 == 180f) direction = Vector3.back;
if(floatVar.z / 4 == 270f) direction = Vector3.left;
if(floatVar.z / 4 == 360f || floatVar.z / 4 == 0f) direction = Vector3.forward;
}
``````

Of course, be careful of this when dealing with floats, especially when comparing!

Edit: link is will now get you to Single.Equals, not Double.Equals. Good catch @troien!