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 Razputin · Jun 27, 2019 at 06:32 AM · if-statementsswitchelseelse if

Need Help Improving If Else statement

I'm trying to think of a way to make it so I can increase / decrease the size of my if/else statement based on the numberOfSprites.

So currently in my example numberOfSprites = 5. But if it were to equal 6 I'd want to add an additional else if for anim.SetFloat("Angle", 5); Or if numberOfSprites = 4 I'd want to reduce the number of else if's and the max angle float would be anim.SetFloat("Angle", 3);

Obviously If/else isn't the way to do this. Is there a way to do something like this?

       if(angle < 180 / numberOfSprites)
         {
             anim.SetFloat("Angle", 0);
         }
         else if (angle > 180 / numberOfSprites && angle < (180 / numberOfSprites) * 2)
         {
             anim.SetFloat("Angle", 1);
         }
         else if(angle > (180 / numberOfSprites) * 2 && angle < (180 / numberOfSprites) * 3)
         {
             anim.SetFloat("Angle", 2);
         }
         else if(angle > (180 / numberOfSprites) * 3 && angle < (180 / numberOfSprites) * 4)
         {
             anim.SetFloat("Angle", 3);
         }
         else
         {
             anim.SetFloat("Angle", 4);
         }
 
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

1 Reply

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

Answer by metalted · Jun 27, 2019 at 07:24 AM

So because the values increment nicely for every next if statement, this problem is perfect for a for loop:

  //Calculate the angle range
     float angleRange = 180 / numberOfSprites;
     
     for(int i = 0; i < numberOfSprites; i++)
     {
         if(angle < angleRange * (i + 1))
         {
             anim.SetFloat("Angle", i);
             break;
         }
     }
     
     //So in your example there are 5 sprites. Lets take a random angle of 85 degrees.
     //The angle range will be 180 / 5 = 36 degrees.
     //The loop will run 5 times total.
     //First run: is the angle smaller than 36 * (0 + 1) = 36 ? -> No so continue.
     //Second run: is the angle smaller than 36 * (1 + 1) = 72 ? -> No so continue.
     //Third run: is the angle smaller than 36 * (2 + 1) = 108 > -> Yes, so set it to ["Angle", 2], which is the same as ["Angle", i]
     //The value is set so break out of the for loop

So because the length of the loop is depending on the amount of sprites, it is infinitely expandable. I'm not able to test this code right now, but I'm pretty sure it works.


After some thinking, I realized that the code could be much simpler than a for loop by just using simple math:

 anim.SetFloat("Angle", Mathf.FloorToInt(angle / (180 / numberOfSprites));
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 Razputin · Jun 27, 2019 at 10:35 PM 0
Share

So simple of course! For some reason my brain kept going to the possibility of switch statements.

avatar image metalted Razputin · Jun 28, 2019 at 05:09 AM 1
Share

It may have been an option, but it would mean hard-coding a lot of cases in advance. Ain't nobody got time for that! There might even be a shorter way than this, because all we're doing is check if the value is in a certain range and apply that "range index" to the Angle. The way I usually do it is like this, with a for loop. But there might be other ways or even functions for this. Always something to discover!

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

108 People are following this question.

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

Related Questions

Any Ideas on how to solve this? 1 Answer

Prevention of air movement 1 Answer

If...else statement with errors 1 Answer

stealthy/not stealthy input detection not working 2 Answers

Storing weapon types and changing between them 1 Answer


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