• 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 kk93 · Jul 02, 2014 at 12:43 PM · angle

Getting a random unit vector2

Hello,

I am trying to find a simple way to get a Vector2 with a magnitude of 1 pointing at a random direction.

One way I thought of doing this was getting a random angle, and then doing matrix multiplication to get a new vector. Something like:

 [  cos theta    sin theta ][0]
 [ -sin theta    cos theta ][1]

which should work fine, but I was wondering if there is a simple built-in static method or conventional way of doing this?

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

3 Replies

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

Answer by Andres-Fernandez · Jul 02, 2014 at 12:53 PM

You can get the vector with whatever values you want (random value for x and y) and then use Vector2.Normalize.

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 kk93 · Jul 02, 2014 at 12:59 PM 0
Share

So... something like this?

 Vector2 randomVector = new Vector2(Random.value, Random.value);
 
 randomVector = randomVector.Normalize();

Would this yield the same probability for getting all angles?

avatar image Andres-Fernandez · Jul 02, 2014 at 01:05 PM 0
Share

Random.value will only give you positive numbers. You can use Random.Range to get a value (and use -1.0f to 1.0f as limits, for example).

For an even easier way, you have Random.insideUnitCircle. Try something like this:

 Vector2 randomVector = Random.insideUnitCircle.Normalize();


avatar image lgarczyn · Sep 30, 2019 at 11:45 PM 0
Share

Very small issue, but if both random numbers are 0, the game will produce an exception.

Because the original vector is spread along a square, the random rotation will also be biased towards the corners of that square.

avatar image
1

Answer by launzone · Dec 20, 2020 at 02:49 AM

Random.insideUnitCircle.normalized;

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
avatar image
5

Answer by lgarczyn · Sep 30, 2019 at 11:48 PM

There are a two problems with the other answer, namely potential exceptions and bias.

A better solution would be something like:

 public Vector2 RandomUnitVector()
 {
     float random = Random.Range(0f, 260f);
     return new Vector2(Mathf.Cos(random), Mathf.Sin(random));
 }

This makes sure that no bias are present, while never throwing any exception.

Comment
Add comment · Show 1 · 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 marplebot · Jan 22, 2021 at 09:20 PM 1
Share

I assume 260 was supposed to be 360. But even so $$anonymous$$athf.Cos and $$anonymous$$athf.Sin are in radians so it should be Random.Range(0f, 2 * $$anonymous$$athf.PI)

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

26 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

Related Questions

The name 'Joystick' does not denote a valid type ('not found') 2 Answers

Having Trouble with Vector3.Angle 1 Answer

Problem with Z rotation when rotating X and Y and the same time. 2 Answers

Quaternion.AngleAxis 1 Answer

Getting the rotation of an object does not return correct rotation 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