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
1
Question by SirSmileX · Jul 21, 2017 at 12:24 PM · randomrigidbody2dvelocitydirection

Rigidbody2D velocity in random direction with exceptions

Hey Guys, so I'm trying to recreate Pong and want the ball to spawn in the middle with a desired start speed. The ball should start being faced in a random direction. However it should not face 0-30, 150-210 and 330-360 degrees. How can I do these exceptions? Here's my code:

 using UnityEngine;
 
 public class Ball : MonoBehaviour {
 
     public float startSpeed;
 
     private Rigidbody2D rb;
 
     void Awake () 
     {
         rb = GetComponent<Rigidbody2D> ();
         rb.velocity = Random.onUnitSphere * startSpeed;
     }
 }
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 Bunny83 · Jul 21, 2017 at 12:41 PM

One straight forward way is to just calculate a random angle for one side and than randomly choose a side:

 float angle = Random.Range(30f, 150f) + Random.Range(0,2) * 180f;

This angle will be in the range you specifed. To turn it into a direction vector, just use:

 float radAngle = angle * Mathf.Deg2Rad;
 Vector2 dir = new Vector2(Mathf.Cos(radAngle), Mathf.Sin(radAngle));

This of course assumes that 0° is the usual mathematical convention of "right". So 90° is "up", 180° is "left" and 270° is "down".

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 SirSmileX · Jul 21, 2017 at 01:47 PM 0
Share

Thank you for this quick help. Works perfectly!

avatar image Bunny83 · Jul 21, 2017 at 02:43 PM 0
Share

Note that this approach only works if you have nice symmetric areas. A more general apporach would be to define "valid" areas, each with a $$anonymous$$ and max value. To get a random direction in any of the valid areas you would first sum up the total range if each sub area and just roll a random value in between 0 and that sum. All you have to do is to distribute the value between those areas.

 pubilc struct Range
 {
     public float $$anonymous$$;
     public float max;
     public float range { get { return max - $$anonymous$$; } }
     public Range(float a$$anonymous$$in, float a$$anonymous$$ax)
     {
         $$anonymous$$ = a$$anonymous$$in;
         max = a$$anonymous$$ax;
     }
 }
 
 List<Range> ranges = new List<Range>();
 range.Add(new Range(30, 150)); // range of 120°
 range.Add(new Range(210, 330)); // range of 120°
 float GetRandomAngle(List<Range> aRanges)
 {
     float total = 0;
     foreach(var r in aRanges)
         total += r.range;  
     float v = Random.Range(0, total); // 0 - 240
     foreach(var r in aRanges)
     {
         if (v < r.range)
             return r.$$anonymous$$ + v;
         v -= r.range;
     }
     return aRanges[0].$$anonymous$$;
 }

Here's a diagram how the final value is deter$$anonymous$$ed:

 //                   v = 163
 // 0            120    |       240
 // ------------------------------
 // |  30 - 150   |  210 - 330   |
 // ------------------------------
 
 v -= 120 --> 43
 
 //    v = 43
 // 0    |       120
 // ---------------
 // |  210 - 330  |
 // ---------------
 
 return 210 + v;  --> 253

This will work with any non-overlapping ranges. They also doesn't need to be ordered in a particular way. This approach is especially useful when the ranges are not symmetric.

avatar image The_MrX_ · Oct 02, 2020 at 01:26 AM 0
Share
 public static Vector3 GetRandomPointAroundTarget(Vector3 point, float desiredDistance, 
             float $$anonymous$$Angle = 0, float maxAngle = 360)
         {
             // 0 Degrees = Right // 90 Degrees = Up  // 180 Degrees = Left // 270 Degrees = Down
             var angleRad = Random.Range($$anonymous$$Angle, maxAngle) * $$anonymous$$athf.Deg2Rad;
             Vector3 dir = new Vector3($$anonymous$$athf.Cos(angleRad), 0, $$anonymous$$athf.Sin(angleRad)).normalized;
             return (dir * desiredDistance) + point;
         }

Turned it into an easy to use function, where you can specify the point around which you want the random direction, with a specific distance.

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

74 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

Related Questions

Issue with direction and rigibody2D 1 Answer

Why is my Rigidbody2D not moving? (C#) 1 Answer

Set Object Rotation based on Collision (2D) 0 Answers

Cannot implicitly convert type `UnityEngine.Vector2' to `float' 1 Answer

rigidbody2d.velocity isn't doing anything. 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