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 /
avatar image
0
Question by TheEmeraldRuby · May 16, 2018 at 04:37 AM · instantiateoverlap

How to prevent 3D object spawn overlapping?

Hello to all. In my game, a bunch of prefabs are randomly placed throughout the map. However, they tend to often spawn on top of each other. I couldn't find a tutorial explaining a fix for 3D objects, so if someone could give me a hand, I would be thankful. Here is my current code:

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class ObstaclePlacer : MonoBehaviour
 {
     public int ObsToPlace = 10;
     public GameObject[] Obstacles = new GameObject[0];
     GameObject Obstacle;
 
     void Awake()
     {
 
         for (int i = 0; i < ObsToPlace; i++)
         {
             Obstacle = Obstacles[Random.Range(0, Obstacles.Length)];
             Vector3 position = new Vector3(Random.Range(-50.0f, 50.0f), 0, Random.Range(-50.0f, 50.0f));
 
             Instantiate(Obstacle, position + Obstacle.transform.position, Quaternion.identity);
         }
     }
 }

Thanks!

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

3 Replies

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

Answer by Darkforge317 · May 16, 2018 at 06:18 AM

I'm on my phone so I can't write code for you. But I can tell you what to do...

Use Physics.OverlapSphere() to check the spawn position before spawning. This method will collect all colliders in the specified area and return them all in an array. Just go through the array in a loop to look for items that you don't want to spawn on top of... Then if you find an item you don't want to spawn on top of, then pick a new random spawn location and repeat the process. (loop)

EDIT:

Try this. You're going to have to play around with the obstacleCheckRadius variable. It determines how big our check area is.

One warning about my code

My code will not spawn an obstacle after 10 attempts. Therefore even if you want 10 obstacles, you might only get 9 if there's not enough space to spawn the last one. I did this to prevent an infinite loop from happening, which would crash your Unity and game.

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class ObstaclePlacer : MonoBehaviour
 {
     public int ObsToPlace = 10;
     public GameObject[] Obstacles = new GameObject[0];
     GameObject Obstacle;
 
     public float obstacleCheckRadius = 3f;
     public int maxSpawnAttemptsPerObstacle = 10;
 
     void Awake()
     {
 
         for (int i = 0; i < ObsToPlace; i++)
         {
             Obstacle = Obstacles[Random.Range(0, Obstacles.Length)];
 
             // Create a position variable
             Vector3 position = Vector3.zero; 
 
             // whether or not we can spawn in this position
             bool validPosition = false;
 
             // How many times we've attempted to spawn this obstacle
             int spawnAttempts = 0;
 
             // While we don't have a valid position 
             //  and we haven't tried spawning this obstable too many times
             while(!validPosition && spawnAttempts < maxSpawnAttemptsPerObstacle)
             {
                 // Increase our spawn attempts
                 spawnAttempts++;
 
                 // Pick a random position
                 position = new Vector3(Random.Range(-50.0f, 50.0f), 0, Random.Range(-50.0f, 50.0f));
 
                 // This position is valid until proven invalid
                 validPosition = true;
 
                 // Collect all colliders within our Obstacle Check Radius
                 Collider[] colliders = Physics.OverlapSphere(position, obstacleCheckRadius);
 
                 // Go through each collider collected
                 foreach(Collider col in colliders)
                 {
                     // If this collider is tagged "Obstacle"
                     if(col.tag == "Obstacle")
                     {
                         // Then this position is not a valid spawn position
                         validPosition = false;
                     }
                 }
             }
 
             // If we exited the loop with a valid position
             if(validPosition)
             {
                 // Spawn the obstacle here
                 Instantiate(Obstacle, position + Obstacle.transform.position, Quaternion.identity);
             }
         }
     }
 }
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 TheEmeraldRuby · Jun 05, 2018 at 05:56 PM 0
Share

@Darkforge317 I tried doing what you said many times, playing around and whatnot, but I just couldn't wrap my head around the concepts, nor could I figure out a way to gt the methods to all work together in the code. It was just too confusing. $$anonymous$$aybe you could give me more specific instructions or some example code to help? I'm rather new to Unity, thank you for understanding.

avatar image Darkforge317 TheEmeraldRuby · Jun 05, 2018 at 11:06 PM 0
Share

It's ok! Thanks for asking! :) I added code to my answer and commented every line that I've added (except for the two extra variables I added on the top of the class).

Let me know if this works and let me know if you're ever confused about it! :)

avatar image Darkforge317 TheEmeraldRuby · Jun 05, 2018 at 11:12 PM 0
Share

Just made a few more edits to it. So if you've already copied it before reading this comment, then just re-copy it. I fixed an infinite loop...

avatar image HunterNacho338 · Apr 23, 2019 at 10:14 PM 0
Share

Thank you for putting in the work to share this it really helped me in my project. Cheers!

avatar image
0

Answer by Cynikal · May 16, 2018 at 10:36 PM

As darkforge mentioned, overlapsphere would work out.

It really depends on the size/shape of the object you're trying to spawn.

On, on your instantiate (psuedo):

Physics.OverlapSphere(or box) at the desired location. Check the collission. If false, you're good to go. If it fails, loop through it again.

Also, add a FailCount, as there will come a point you just need to fail the operation when too many loops fail, if you don't, you'll eventually freeze your game due to a constant running process.

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
0

Answer by Eno-Khaon · May 16, 2018 at 11:13 PM

Take a look at Physics.ComputePenetration().

The example script includes the general information necessary to be able to check for nearby objects, then if they are nearby, determine the minimum separation required to keep them apart from each other.

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.

Follow this Question

Answers Answers and Comments

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

How to not instantiate an object if there is another object there? 1 Answer

How to compare and detect overlapping gameObject Prefabs using maths 0 Answers

Checking if object intersects? 1 Answer

Deleting objects when they overlap other objects? 1 Answer

Can anyone help check for colliders in this spawning script so that the spawns don't overlap each other or spawn inside of walls, objetcs etc 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