• 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
5
Question by torg · Nov 26, 2009 at 01:32 PM · gameobjectrandom

Avoiding overlap between randomly placed object

Is there any smart and efficient methods that could be used to avoid collision and overlap between gameobjects generated and placed during runtime and already existing objects in the scene?

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

5 Replies

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

Answer by TowerOfBricks · Nov 26, 2009 at 02:59 PM

Take a look at Physics.CheckSphere: http://unity3d.com/support/documentation/ScriptReference/Physics.CheckSphere.html/ Physics.CheckSphere

First you generate a random position, then you use CheckSphere to find out if there is an object there already, if it is, generate a new position, if not, place the object there.

This won't look artificial and the objects wont overlap.

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 Ehren · Dec 10, 2009 at 06:29 PM 1
Share

FYI, The CheckSphere link has an extra slash at the end.

avatar image
3

Answer by Statement · Nov 26, 2009 at 01:45 PM

Make sure there is enough space to start with.

Also, if you're instansiating alot of object inside a volume you can use cells to place objects semi-randomly.

Imagine you've got a grid x*y*z large, and each grid might be like 10x10x10 units. Then randomly select n amount of cells to generate items into, and randomize the position of the object inside that cell. This will greatly aid instansiating objects somewhat randomly that won't overlap as long the object fit inside the cell. Remember you still have to make sure that none of the objects extents (bounding box) pass over to neighboring cells.

In a dense grid, this effect will look very artificial but in a sparse grid this is a good approach.

Note that if you register existing game objects as occupants of cells, then you'll also gurantee that you aren't instansiating objects into preexisting objects.

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
3

Answer by Ash · Nov 26, 2009 at 03:53 PM

I used Check Sphere and it works fine, make sure you have an exit strategy if somehow there is no space to place the object, avoiding an infinate loop!

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
1

Answer by torg · Nov 26, 2009 at 08:54 PM

I've had some time to play with this now and must say I'm a little lost with how this exactly works. I'm using Physics.CheckCapusle as a capsule like shape is more in line with my objects, which are sometimes long and sometimes not.

static function CheckCapsule (start : Vector3, end : Vector3, radius : float, layermask : int = kDefaultRaycastLayers) : bool

I'm assuming that the "start" vector would be the position of one end of my object and "end" the other. The radius is self explanatory, and as I understand it the layermask is an option to ignore collisions with objects on said layer? My object are being placed on a terrain made of a planes so naturally I put these planes on this layer, which gives me only false results (even if the object are clearly colliding. And if I don't set this layermask I get only positives regardless of object collision. Could anyone point me in the right direction? :) Btw. Each object is made up of several children, sort of on a row of different length, so the way I figure out start and end positions is through the position of the first and last child object.

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 TowerOfBricks · Nov 27, 2009 at 07:09 AM 0
Share

You can iterate through all the child objects and test which one is placed most to the right for example, or you can use the collider.bounds and figure out which direction is the longest, and from that generate a start and end value.

Regarding the layer mask. The best way is to define it in the editor, and make sure both the objects and the plane ground are set to different layers.

avatar image
0

Answer by torg · Nov 26, 2009 at 03:14 PM

Thanks for some excellent suggestions. Already using a grid system of sorts, but the objects in question are fairly large and of different sizes so its difficult to fit them perfectly into a grid without making the spacing excessive. I'm also giving them a random rotation, which makes things even trickier, but the Physics.CheckSphere looks like a good solution to my problem :)

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.

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

No one has followed this question yet.

Related Questions

C# GameObjects Instantiate Into Each Other Issue 1 Answer

GameObject variable change for all of the objects 1 Answer

Three Spots For Three Random Objects 1 Answer

How to randomly spawn three non repeating gameobjects from an array? 2 Answers

Instantiate a GameObject at the position of one of its child objects 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