Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 josephscarry · May 03, 2014 at 06:06 PM · random.rangerandomize

Are there better ways to create random behavior?

Hello,

I'm just getting my feet wet with Unity and following some tutorials to get a grasp of the basics. I'm currently coding a "ball" object to start at random. I've successfully done so but was wondering if there is a better or more concise code to achieve the same effect or even better randomize the object's behavior. The current code:

 function GoBall () {
     var randomNumber = Random.Range(0, 14);
     if (randomNumber <= 0.5) {
         rigidbody2D.AddForce (new Vector2 (ballSpeed, 10));
     }
     else if (randomNumber <= 1) {
         rigidbody2D.AddForce (new Vector2 (ballSpeed, 60));
     }
     else if (randomNumber <= 2) {
         rigidbody2D.AddForce (new Vector2 (-ballSpeed, -60));
     }
     else if (randomNumber <= 3) {
         rigidbody2D.AddForce (new Vector2 (ballSpeed, 30));
     }
     else if (randomNumber <= 4) {
         rigidbody2D.AddForce (new Vector2 (-ballSpeed, -30));
     }
     else if (randomNumber <= 5) {
         rigidbody2D.AddForce (new Vector2 (ballSpeed, 20));
     }
     else if (randomNumber <= 6) {
         rigidbody2D.AddForce (new Vector2 (-ballSpeed, -20));
     }    
     else if (randomNumber <= 7) {
         rigidbody2D.AddForce (new Vector2 (-ballSpeed, 10));
     }
     else if (randomNumber <= 8) {
         rigidbody2D.AddForce (new Vector2 (-ballSpeed, 60));
     }
     else if (randomNumber <= 9) {
         rigidbody2D.AddForce (new Vector2 (ballSpeed, -60));
     }
     else if (randomNumber <= 10) {
         rigidbody2D.AddForce (new Vector2 (ballSpeed, -30));
     }
     else if (randomNumber <= 11) {
         rigidbody2D.AddForce (new Vector2 (-ballSpeed, 30));
     }
     else if (randomNumber <= 12) {
         rigidbody2D.AddForce (new Vector2 (-ballSpeed, 20));
     }
     else if (randomNumber <= 13) {
         rigidbody2D.AddForce (new Vector2 (ballSpeed, -20));
     }
     else {
         rigidbody2D.AddForce (new Vector2 (-ballSpeed, -10));
     }
     

Thanks for any help!

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

Answer by robertbu · May 03, 2014 at 06:13 PM

If you want a random direction and speed, you can do:

  rigidbody2D.AddForce(Random.InsideUnitCircle * maxSpeed);

If you want a random direction and fixed speed you can do:

 rigidbody2D.AddForce(Random.InsideUnitCircle.normalized * ballSpeed);

Note that Random.Range() using integers is exclusive of the final value, so Random.Range(0, 14) produces the values 0 through 13, but not 14. It also only produces integers. This means that your final 'else' would never be executed. For what you are doing, use the floating point version:

 var randomNumber = Random.Range(0.0, 14.0);

This will produce the full range of values.

If you want these specific values but different/simpler code, you's either have to 1) find a pattern, 2) put the values in an array, or 3) use some sort of curve to fit the values.

Comment
Add comment · 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

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

20 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

Related Questions

How do i tell what gameobject im on 1 Answer

Random.Range doesnt work anymore 4 Answers

Randomize 2 clips from an AudioSource List by name? 1 Answer

Putting answer randomly in options 1 Answer

Random.Range is not changing? 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