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 /
  • Help Room /
avatar image
0
Question by mertbevell · Jan 10, 2017 at 06:47 PM · randomspawnintersect

How to randomly spawn objects in one axis without them touching eaching other?

Hey, so the randomizing part is easy but I couldn't get the objects (with Colliders) not touch each other while spawning at a new location (imagine background mountains that shuffle and relocate in an endless runner) I tried to use Bounds.Intersects but failed.. please take a look:

 Bounds spawnBounds = this.GetComponent<Renderer> ().bounds;
 
         for(int i=0; i<backgrounds.Length; i++) {
             Transform aBackground = backgrounds [i];
             Transform pBackground;
             if (i == 0) {
                 pBackground = aBackground;
             } else {
                 pBackground = backgrounds [i - 1];
             }
 
             if (!aBackground.gameObject.GetComponent <Renderer> ().bounds.Intersects (pBackground.gameObject.GetComponent <Renderer> ().bounds)) {
                 aBackground.position = new Vector3 (Random.Range (spawnBounds.min.x, spawnBounds.max.x), aBackground.position.y, aBackground.position.z);
                 print ("not intersecting");
             
 } else {
                 aBackground.position = new Vector3 (pBackground.gameObject.GetComponent <Renderer>().bounds.max.x+1.0f, aBackground.position.y, aBackground.position.z);
 
             }
         

alt text

the green bounding box belongs to the 'spawnBounds' object which the mountain boxes are its children..

Thanks!

screen-shot-2017-01-10-at-14536-pm.png (407.9 kB)
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
0

Answer by Scoutas · Jan 10, 2017 at 07:03 PM

If every single 'mountain' object would have a BoxCollider object inside, and every single object would be inside of a list, you could do this:

1) Put the random coordinates for the mountain inside variables.

2) Say, you're randomizing the size of the box as well. Store the sizes of the extens somewhere as well. (You'd only really need the x axis extents.)

3) Now, once you've placed them all, loop through the list of the background objects that you have and do a check, if the object's left and right sides are not inside any other object. e.g.

You could use Physics.CheckBox, passing in the random position and random extents for the box and if it returns true, it would mean that the new object would intersect it. If so, generate new random position and new random extents, and try it again. If it returns false (as in it doesn't intersect anything), instantiate the object there.

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 Scoutas · Jan 10, 2017 at 07:13 PM 0
Share

You could check this Coding Challenge by $$anonymous$$ Shiffman, you could use similar logic used there, making the extents grow, until they intersect and then instantiate the backgrounds, so ther wouldn't be any holes between the objects.

avatar image mertbevell · Jan 13, 2017 at 02:06 PM 0
Share

Thanks a lot @Scoutas these are good points, I'll try to implement Physics.Checkbox, as the Renderer.Bounds.Intersects doesn't seem to work for me..

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

90 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 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

Spawning objects at runtime in vertical-scroller game 0 Answers

Trying to make a field of meteors forever, but I can tell I'm doing it horribly 0 Answers

Spawn enemies so they aren't instantiated on top of each other (C#) 2 Answers

Spawn gameObject horde, modify concentration of spawned objects. 1 Answer

Random Object Spawn 0 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