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 zannghast · Sep 15, 2010 at 06:12 AM · randomcircle

Random Coordinates Outside a Circle

Hi all,

I understand that there's a Random.insideUnitCircle variable to get a random 2D coordinate inside a theoretical circle.

But what if I wanted to get a random 2D coordinate outside of that theoretical circle? Any ideas on how to go about that?

Thanks in advance,

Dendro

Comment
Add comment · Show 1
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 · Sep 15, 2010 at 06:34 AM 0
Share

I've answered assu$$anonymous$$g that you know the $$anonymous$$/max bounds of the screen (or positions that are valid for your random 2D coordinate). If this is not the case, please update your question with further details. Cheers.

1 Reply

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

Answer by · Sep 15, 2010 at 06:33 AM

Broadly, this is what I would do:

  1. Randomise a position within the min/max bounds
  2. Check distance from the centrepoint to the randomised position
  3. If it's less than a desired radius, recalculate the random position

Be aware that sqrMagnitude is much faster than Vector2.magnitude. To use this, you will then obviously need to square the radius value. For example:

if ( (randomPos.position - centrePoint.position).sqrMagnitude <= radius*radius )
{ // retry }
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 zannghast · Sep 16, 2010 at 12:52 AM 0
Share

Thanks $$anonymous$$arowi. I'm not entirely sure on how to 'reply to an answer', and I hope I'm doing it right. I'll try out what you've suggested. Just a thought, though, isn't the logic you are suggesting quite similar to a brute-force solution? Thanks again.

avatar image · Sep 16, 2010 at 01:10 AM 0
Share

Comments are perfect for replying! It is fairly brute-force, but to my knowledge there's no built-in functionality to find a point inside a rectangle and outside of a circle. Perhaps that was the answer you were after, but I thought I'd make a suggestion on how you could go about it. If you get stuck with any of the scripting, I can take a look at what you have if you'd like.

avatar image zannghast · Sep 20, 2010 at 06:33 AM 0
Share

Hi $$anonymous$$arowi, really sorry for the superbly late feedback on this. Although I wasn't able to impplement your code solution, I did use the concept you had suggested and came up with a rather cruder (if there is such a word hehe..) way of generating points outside a circle. To that extent, I do believe that the concept you suggested still answered my question. Thanks again!

btw: how do you post an answer to your own question?

avatar image · Sep 20, 2010 at 06:39 AM 0
Share

Hey, no worries at all. It's quite common for people to ask a question and never return, so I'm just glad you're back! There should be a button at the bottom of the page that says, "Answer Your Question". Pressing that (and then confir$$anonymous$$g the dialogue) will cause the answer box to appear. I've got some time this week, so if you'd like help with the script, feel free to update your question with what you have and I'll help where I can!

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

No one has followed this question yet.

Related Questions

random.onUnitCircle ? 6 Answers

Spawn GameObjects randomly on a Circle Collider2D 1 Answer

Gaussian Distribution in circle on x and y axis 1 Answer

How to change the rotation of the circle spawn? 1 Answer

Why does inside unit circle use the world point? 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