• 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 /
  • Help Room /
avatar image
0
Question by mullac51 · Feb 13, 2017 at 01:45 PM · randomif-statementsrandom.rangeifif-else

Randomly choosing between 3 options

Basically i want the character to either jump up and left, up and right or just straight up.

Sorry if it' simple to do, i'm new to this

Here is my code so far:

 if (Random.Range(0, 2) == 0)
                 rb.AddForce(Vector2.up * upforce, ForceMode2D.Impulse);
                 rb.AddForce(Vector2.left * directionforce, ForceMode2D.Impulse);
             else
                 rb.AddForce(Vector2.up * upforce, ForceMode2D.Impulse);
                 rb.AddForce(Vector2.right * directionforce, ForceMode2D.Impulse);
             else
                 rb.AddForce(Vector2.up * upforce, ForceMode2D.Impulse);

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
2
Best Answer

Answer by Chikari · Feb 13, 2017 at 01:57 PM

     switch(Random.Range(0,3))
     {
     case 0:
         rb.AddForce(Vector2.up * upforce, ForceMode2D.Impulse);
         rb.AddForce(Vector2.left * directionforce, ForceMode2D.Impulse);
         break;
     case 1:
         rb.AddForce (Vector2.up * upforce, ForceMode2D.Impulse);
         rb.AddForce (Vector2.right * directionforce, ForceMode2D.Impulse);
         break;
     case 2:
         rb.AddForce (Vector2.up * upforce, ForceMode2D.Impulse);
         break;
     }
Comment
Add comment · Show 4 · 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 PizzaPie · Feb 13, 2017 at 03:33 PM 0
Share

Random.Range should be Random.Range(0,3) if you want it to reach the case 2. (int)Random.Range will exclude the max value.

avatar image Chikari · Feb 13, 2017 at 03:42 PM 0
Share

No. See the documentation: "Returns a random float number between and $$anonymous$$ [inclusive] and max [inclusive]. "

It's odd, but correct.

avatar image PizzaPie Chikari · Feb 13, 2017 at 03:56 PM 1
Share

For floats yes but not for integers, scroll down to the doc. In this case it is an int so it will exclude the max, if it was float it would hardly ever reach any case.

avatar image Chikari PizzaPie · Feb 13, 2017 at 03:59 PM 1
Share

Thanks for the heads up! I have adjusted my answer.

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

97 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

Related Questions

How do I generate a random number in regular time intervals? 0 Answers

Random.Range code problem or problem with Unity? 1 Answer

how to randomly instantiate prefabs but each with it's own probability? 2 Answers

The If statement condition is false but the if statement stills executes 1 Answer

Can I generate values outside of the Random.insideUnitCircle?? 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