Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by vittu1994 · Apr 21, 2016 at 05:17 PM · switchswitch-case

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!

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

2 Replies

· Add your reply
  • Sort: 
avatar image
2
Best Answer

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.

Comment
Add comment · Show 2 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image vittu1994 · Apr 21, 2016 at 06:35 PM 0
Share

Thanks a bunch! i will try this out!

avatar image vittu1994 · Apr 21, 2016 at 06:53 PM 1
Share

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!

avatar image
0

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!

Comment
Add comment · Show 3 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image vittu1994 · Apr 21, 2016 at 06:41 PM 0
Share

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?

avatar image Aenigmaticus vittu1994 · Apr 21, 2016 at 07:00 PM 0
Share

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!

avatar image troien · Apr 21, 2016 at 07:08 PM 0
Share

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

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

6 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Switch statement not running on every frame 1 Answer

I would like some help refactoring my code, I am working with switch cases concering the delivery of food items in my game 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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges