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 /
This question was closed Apr 23, 2020 at 01:57 PM by EmirhanYilmaz for the following reason:

Solved

avatar image
0
Question by EmirhanYilmaz · Apr 22, 2020 at 11:47 AM · 2drandomrandomspawning

How to spawn randomly positioned objects without overlapping

Hi, I can spawn objects randomly but sometimes they overlapping. I want to spawn objects without overlapping how can I do it? My spawn code:

 public void StartGame() {
         count = Random.Range(2, 6);
 
         for(int i = 0; i < count; i++) {
             float x = Random.Range(-2.0f, 2.0f);
             float y = Random.Range(-4.0f, 4.0f);
 
             Instantiate(enemy, new Vector3(x, y, 0), Quaternion.identity);
         }
     }
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

2 Replies

  • Sort: 
avatar image
0
Best Answer

Answer by MaxLevelNoob · Apr 22, 2020 at 01:43 PM

 public void StartGame()
 {
     count = Random.Range(2, 6);
     float enemyRadius = enemy.GetComponent<Collider2D>().bounds.extents.x;

     for (int i = 0; i < count; i++)
     {
         float x = Random.Range(-2.0f, 2.0f);
         float y = Random.Range(-4.0f, 4.0f);
         Vector2 spawnPoint = new Vector2(x, y);

         //Assuming you are 2D
         Collider2D CollisionWithEnemy = Physics2D.OverlapCircle(spawnPoint, enemyRadius, LayerMask.GetMask("EnemyLayer"));

         //If the Collision is empty then, we can instantiate
         if (CollisionWithEnemy == false)
             Instantiate(enemy, new Vector3(x, y, 0), Quaternion.identity);
     }
 }
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 MaxLevelNoob · Apr 22, 2020 at 01:52 PM 0
Share

Basically using Physics2D to check for any collisions around your spawnPoint, and if there isn't, we'll instantiate, otherwise we won't.

avatar image
0

Answer by yummy81 · Apr 22, 2020 at 02:39 PM

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class Test : MonoBehaviour
 {
     public GameObject enemy;
     public float distanceBetweenObjects = 2f;
     List<Vector3> points = new List<Vector3>();
     
     void Start(){
         ObjectPositioning();
         ObjectSpawning();
     }
     
     void ObjectPositioning(){
         int count = Random.Range(2, 6);
 
         for(int i = 0; i < count;) {
             float x = Random.Range(-2.0f, 2.0f);
             float y = Random.Range(-4.0f, 4.0f);
             Vector3 point = new Vector3(x, y, 0);
             
             if (points.Count==0){
                 points.Add(point);
                 i++;
                 continue;
             }
             
             for(int j = 0; j < points.Count; j++){
                 
                 if ((point-points[j]).sqrMagnitude > distanceBetweenObjects * distanceBetweenObjects){
                     if (j==points.Count-1){
                         points.Add(point);
                         i++;
                     }
                     continue;
                 }            
                 break;
             }    
         }
     }
 
     void ObjectSpawning(){
         for (int i = 0; i < points.Count; i++){
             Instantiate(enemy, points[i], Quaternion.identity);
         }
     }
 }
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

Follow this Question

Answers Answers and Comments

262 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 avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image 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

Spawn objects at random position ON a 2D object. 1 Answer

Auto Creation of Objects 1 Answer

How to add randomness in the following code snippet? 0 Answers

OnMouseDown generates random sprite 1 Answer

Randomly Generated 2D Level 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