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 beginningsir · Aug 13, 2014 at 11:17 AM · javascriptpositionrandomoverlap

Random position without overlap. HELP!

I need a JavaScript coding reference on how to randomly position several objects (8 to 10 objects) on a specific 2D area without overlapping them. Once one of these objects gets a mouse click it would reposition itself onto a new random position (within the same area, without overlapping the other objects). I am fairly new to coding and this is one of my practice exercises. I would be really glad if someone could help me out with this, I've done courses and have been researching for a long time to get this right... with no success...

Comment
Add comment · Show 2
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 Josh Naylor ♦♦ · Aug 13, 2014 at 11:19 AM 0
Share

You could place all the object in an array then cycle through them and use the Random function (http://docs.unity3d.com/ScriptReference/Random.Range.html) to place them in a certain range on a 2d plane.

avatar image beginningsir · Aug 13, 2014 at 01:03 PM 0
Share

Thank you Josh! :)

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by smoggach · Aug 13, 2014 at 01:28 PM

There are much more elegant ways to do it but the most obvious way to me would be some code that follows this logic: 1. pick a random position 2. perform a boxcast the same size as your object on that position 3. place your object if there are no hits 4. if there are hits then choose a new random position and repeat

Comment
Add comment · Show 2 · 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 Owen-Reynolds · Aug 13, 2014 at 01:42 PM 0
Share

The problem I've have with this is there's always some way to position the first N-1 objects so the last one won't fit, or will fit in only a very small area.

So, you limit to 50 tries and take the last if none work. But the last is often a complete overlap with something else, so you have to keep track of the "best" (least overlappy) result as you go.

I've only gotten good results with this when there was a lot of extra space.

avatar image beginningsir · Aug 13, 2014 at 02:20 PM 0
Share

Thank you very much. This helped the cause! :D

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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

Spawn game object in random position on screen 1 Answer

Spawn GameObjects without overlap 1 Answer

Randomly generated number 0 Answers

How do I check if/where in a sprite another sprite is? 0 Answers

Quaternion.identity problem at random spawn 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