Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
12 Jun 22 - 14 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 /
This question was closed Jun 20, 2019 at 11:30 AM by ratchetunity for the following reason:

Other

avatar image
0
Question by ratchetunity · Jun 12, 2019 at 02:54 PM · instantiatepositionrandomoverlapoverlapping

Spawn GameObjects without overlap

Hi! I'm trying to spawn some gameobjects in a chosen area without overlap among them. I save each object position (plus the collider's radius) in 3 lists (one per axis) so the position can not be taken again, but it's not working. This is my code:

 [SerializeField] GameObject nodePrefab;
 [SerializeField] int numberOfObjects;
 [SerializeField] float xValue;
 [SerializeField] float yValue;
 [SerializeField] float zValue;
 private Vector3 spawnPosition;
 private float radiusValue;
 List<float> xPositionList = new List<float>();
 List<float> yPositionList = new List<float>();
 List<float> zPositionList = new List<float>();

 private void Awake()
 {
     radiusValue = nodePrefab.GetComponent<SphereCollider>().radius + 0.15f;
 }

 private void Start () {
     for (int i = 0; i < numberOfObjects; i++)
     {
         float xPos = Random.Range(-xValue, xValue);
         float yPos = Random.Range(-yValue, yValue);
         float zPos = Random.Range(-zValue, zValue);
         while (xPositionList.Contains(xPos)) xPos = Random.Range(-xValue, xValue);
         while (yPositionList.Contains(yPos)) yPos = Random.Range(-yValue, yValue);
         while (zPositionList.Contains(zPos)) zPos = Random.Range(-zValue, zValue);
         spawnPosition = new Vector3(xPos, yPos, zPos);
         Instantiate(nodePrefab, spawnPosition, Quaternion.identity, transform);
         xPositionList.Add(xPos + radiusValue);
         yPositionList.Add(yPos + radiusValue);
         zPositionList.Add(zPos + radiusValue);
         xPositionList.Add(xPos - radiusValue);
         yPositionList.Add(yPos - radiusValue);
         zPositionList.Add(zPos - radiusValue);
     }
 }
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

  • Sort: 
avatar image
0
Best Answer

Answer by ernest326648 · Jun 12, 2019 at 06:45 PM

You can use Physics.CheckSphere(position, radius, layermask(optional)) instead of a sphere collider. It works the same way as raycasting. If it collides with another objects it will return a value of true else it will return false. You can also put a layermask on the gameobjects and check if its colliding with another.

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 ratchetunity · Jun 13, 2019 at 07:16 AM 0
Share

Awesome! Thank you @ernest326648 !

avatar image ernest326648 ratchetunity · Jun 13, 2019 at 06:55 PM 0
Share

Your welcome :)

Follow this Question

Answers Answers and Comments

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

Checking if object intersects? 1 Answer

instantiate prefabs y+1 from last instintiated prefab 1 Answer

spawn muzzle flash with random rotation? 2 Answers

Instantiate Random 3D Objects/platforms that dont overlap 1 Answer

avoid overlap problem when randomly instantiating 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