- Home /
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!
Answer by Scribe · Apr 21, 2016 at 06:12 PM
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.
Okay i tried it and made it work! Thanks alot!
void RotateShip()
{
if(Input.Get$$anonymous$$ouseButtonDown(1) && stateCount == 0)
{
Debug.Log("rotation");
Rotation(new Vector3(0, 0, 90 * rotationCount));
rotationCount = (rotationCount + 1) % 4;
}
}
private void Rotation(Vector3 currRot)
{
transform.eulerAngles = new Vector3(currRot.x, currRot.y, currRot.z);
}
I also had to change rotationCount to 1 or else i had to tap twice to get it to rotate once (after that it worked as intended, before it was 0). But now with how the new code looks and rotationCount=1 it works as intended!
Answer by Aenigmaticus · Apr 21, 2016 at 06:13 PM
@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. Click here for more about the Modulus operator
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!
Thanks for the answer. I think i dont need to add f because im changing the rotation (angle) and not the position. If i were to assign a new position for my object i would use f. I am using VS right now, does monodevelop notifiy you when not using f in vector3?
I currently use Visual Studio... Ok! This makes sense; the Vector3 initializer "public Vector3(args){}" can be defined multiple times in its class definition using different argument types. In this case, it is... cool!
For the float part. both monodevelop and vs need to have f beind it if you want it to be a float, that's how C# works. If you do not use f, it will be either int or double.
In this case, If you don't use f behind it, the C# compiler will see it as an int. And sinse int can be casted implicitly to float this is usually fine. Double can't be implicitly casted to float (Because you'll lose data) and therefore you do get an error when you say 90.0, but not when you say 90 or 90.0f
Your answer
Follow this Question
Related Questions
Switch statement not running on every frame 1 Answer
Trying to check "if >" in a switch statement 2 Answers
How do switch() statements work? 0 Answers
Variable usage in Switch Statements 2 Answers