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
1
Question by Jan_Julius · Sep 23, 2014 at 08:28 AM · instantiateraycastcollider

What would be a good way to not have objects spawn inside one another?

I've tried quite a few things, trying to spawn them on other heights etc, but there can be more objects spawning at the same time but I dont want them to spawn inside eachother because they arrange once they can raycast on eachother, which is impossible when they spawn inside eachother and this gets my objects stuck, what would be a good way?

Objects all spawn on the same x and z axis, how can I make sure they dont spawn in the same height? even when instantiated at the same time? spawning them goes from a global script.

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 jokim · Sep 23, 2014 at 02:11 PM 0
Share

Does it have to be random locations ? Can't you force them to spawn where you want them ?

If their X and Z are the same, you could just easily put a growing Y number to instantiate. just make sure that Y value is bigger than the object height

2 Replies

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

Answer by dmg0600 · Sep 23, 2014 at 08:59 AM

You could use Physics.OverlapSphere:

 void InstantiateNewObject(GameObject prefab, int minimumY, int maximumY)
 {
     Vector3 positionToInstantiate;
     Collider[] hitColliders;
     
     do
     {
         positionToInstantiate = Random.Range(minimumY, maximumY);
         hitColliders = Physics.OverlapSphere(positionToInstantiate, objectHeight);
     } while (hitColliders.Length > 0);
 
     Instantiate(prefab, positionToInstantiate, Quaternion.identity);
 
 }

Take into account that maximumY won't ever be assigned as it is exclusive. Just use InstantiateNewObject whenever you want to instantiate a new object.

Comment
Add comment · Show 12 · 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 Jan_Julius · Sep 23, 2014 at 09:04 AM 0
Share

This seems interesting, I will have a go at it and see if it fits my needs then I will come back to this. :).

avatar image Jan_Julius · Sep 23, 2014 at 12:10 PM 0
Share

I don't know what I'm doing wrong or if the code doesn't work correctly, but my Unity freezes when I use it, also the X and Z axis are predeter$$anonymous$$ed.

Also, what does positionToInstantiate store on the x and z axis are those randomized aswell?

avatar image dmg0600 · Sep 23, 2014 at 12:20 PM 0
Share

Right! I didn't initialize those. Use this:

 void InstantiateNewObject(GameObject prefab, int $$anonymous$$imumY, int maximumY, int tries)
 {
     Vector3 positionToInstantiate = new Vector3(yourX, 0, yourZ);
     Collider[] hitColliders;
  
     do
     {
         positionToInstantiate.y = Random.Range($$anonymous$$imumY, maximumY);
         hitColliders = Physics.OverlapSphere(positionToInstantiate, objectHeight);

         --tries;
     } while (hitColliders.Length > 0 && tries > 0);

     if (tries > 0)
         Instantiate(prefab, positionToInstantiate, Quaternion.identity);
  
 }

If it freezes is because it does not find a place to instantiate. I have put a tries so if in that number of tries doesn't find a place, it won't be instantiated.

avatar image Jan_Julius · Sep 23, 2014 at 01:47 PM 0
Share

But it has to always happen, if it can't happen it has to happen a split second later? I don't quite understand what's going on.

avatar image dmg0600 · Sep 23, 2014 at 01:56 PM 0
Share

Then put it like this:

 void InstantiateNewObject(GameObject prefab, int $$anonymous$$imumY, int maximumY)
 {
     Vector3 positionToInstantiate = new Vector3(yourX, 0, yourZ);
     Collider[] hitColliders;
  
     do
     {
         positionToInstantiate.y = Random.Range($$anonymous$$imumY, maximumY);
         hitColliders = Physics.OverlapSphere(positionToInstantiate, objectHeight);
     } while (hitColliders.Length > 0);
  
     Instantiate(prefab, positionToInstantiate, Quaternion.identity);
  
 }

But be aware that if it does not find a place to Instantiate it will freeze.

Another approach is to instantiate it even if it does not find an empty place in the number of tries that you have said, if you like this better, do this:

 void InstantiateNewObject(GameObject prefab, int $$anonymous$$imumY, int maximumY, int tries)
 {
     Vector3 positionToInstantiate = new Vector3(yourX, 0, yourZ);
     Collider[] hitColliders;
  
     do
     {
         positionToInstantiate.y = Random.Range($$anonymous$$imumY, maximumY);
         hitColliders = Physics.OverlapSphere(positionToInstantiate, objectHeight);
  
         --tries;
     } while (hitColliders.Length > 0 && tries > 0);
 
     Instantiate(prefab, positionToInstantiate, Quaternion.identity);
  
 }

Show more comments
avatar image
0

Answer by 767_2 · Sep 23, 2014 at 08:46 AM

you can make an array of bools to make it true if an object is instantiated on that Y value bool[] height;

 spawn(int y){
 if(height[y]==false){
  instantiate()
  height[y]=true;
   }
 }
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 Jan_Julius · Sep 23, 2014 at 08:47 AM 0
Share

BUt lets say I make an array of 0, 2, 4, 6, 8, 10. If I have 8 objects spawning at the same time and while number 10 is falling I start back at number 0, so number 0 spawns inside the falling number 10?

avatar image 767_2 · Sep 23, 2014 at 08:56 AM 0
Share

you can spawn when nothing is falling

avatar image Jan_Julius · Sep 23, 2014 at 09:03 AM 0
Share

But I need multiple objects spawning (they will instantly fall)

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

28 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

Related Questions

Unity Raycast2D ignored instantiated Prefab object 0 Answers

Raycast causes game to crash when shooting up 1 Answer

how to move an object clone to mouse position, i dont understand ? 1 Answer

Stop platforms spawning inside player 1 Answer

Get an Object to Face/Move to RayCast Hit.point? 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