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
0
Question by KV-2 · Jun 05, 2013 at 12:14 PM · c#randomspawnarrays

Problem with random generated objects(intersects objects)

Hello! I apologize for my terrible English, but I have the important question.
I have a problem. When generating facilities, some of them - cross each.How to fix it?

 public int maxSisePoint = 25;  
 public int minSizePont = 15;  
 GameObject[] InstanceList;  
 public int SizePoint;  
 void Start (){  
 int SizePoint = Random.Range(minSizePont,maxSisePoint);  
 InstanceList = new GameObject[SizePoint];  
 for (int i = 0; i < SizePoint; i++){  
 int addXPos = Random.Range(-200, 200);   
 int addZPos = Random.Range(-200, 200);  
 GameObject item = Instantiate(Resources.Load("_Prefabs/prefab_CubePoint")) as GameObject;  
 item.transform.parent = transform;  
 InstanceList[i] = item;  
 InstanceList[i].transform.Translate(addXPos, 0, addZPos);  
 InstanceList[i].name= ""+(i+1);  
 }  
 }

   

Tried to check it, but does not work when it should.

 for (int j = 0; j < InstanceList.Length; j++){  
 for (int i = 1; i< InstanceList.Length; i++){  
 if (InstanceList[j].renderer.bounds.Intersects(InstanceList[i].renderer.bounds)&i != j)  
 Debug.Log("* What is there to do? *");  
 }}    

Help me pls!

Comment
Add comment · Show 2
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 robertbu · Jun 05, 2013 at 03:45 PM 0
Share

The brute force approach to this problem is that you would repeatedly move the currently placed object until you find a position in which the bounds did not intersect. Note usually you limit the number of tries to some fixed number, since the possibility exists that there is no position that would work (and you don't want your program to hang).

avatar image KV-2 · Jun 05, 2013 at 05:05 PM 0
Share

well, and how do I do this?

1 Reply

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

Answer by robertbu · Jun 05, 2013 at 05:26 PM

I would restructure the code something like this:

 using UnityEngine;
 using System.Collections;
 
 public class Bug20 : MonoBehaviour {
     
     public int maxSisePoint = 25;  
     public int minSizePont = 15;  
     GameObject[] InstanceList; 
     private int created = 0;
     public int SizePoint;  
     void Start () {  
         
         int SizePoint = Random.Range(minSizePont,maxSisePoint);  
         InstanceList = new GameObject[SizePoint];  
         for (int i = 0; i < SizePoint; i++){  
      
             GameObject item = Instantiate(Resources.Load("_Prefabs/prefab_CubePoint")) as GameObject;  
             item.transform.parent = transform;  
             Vector3 v3Org = item.transform.position;
                 
             for (int j = 0; j < 100; j++) {
                 item.transform.position = v3Org;
                 int addXPos = Random.Range(-200, 200);   
                 int addZPos = Random.Range(-200, 200);
                 item.transform.Translate(addXPos, 0, addZPos); 
                 if (BoundsCheck(item.renderer.bounds))
                         break;
             }
             
             InstanceList[i] = item;  
             InstanceList[i].name= ""+(i+1);
             created++;
         }
     }  
 
     
     bool BoundsCheck(Bounds bounds) {
         for (int i = 0; i < created; i++){    
             if (bounds.Intersects(InstanceList[i].renderer.bounds))
                     return false;
         }
         return true;
     }
 }

The code tries 100 times to position an object so that it does not intersect. I've never used the bound.intersect in this way. You may have to go to a model where objects are not placed within a certain radius of each other. Also there are several places in this code where I would do things differently, but since I don't know what you are doing, I did not want to make too many changes.

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

14 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

Related Questions

C# Random spawn from an array 0 Answers

Spawn enemies so they aren't spawned on top of each other (C#) 1 Answer

Enemies spawn on top of each other(C#)(Unity) 1 Answer

How to make enemy prefab spawn at random times between 1.0f to 10.0f. 1 Answer

AI spawning areas. What are the ways to make them? 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