Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Question Instatntiting objects keep overlapping

Discussion in 'Scripting' started by Jinngineer, Nov 22, 2021.

  1. Jinngineer

    Jinngineer

    Joined:
    Apr 22, 2020
    Posts:
    41
    Hello all, So im wondering how to get my objects from overlapping during instantiating. I found this interesting way to do it, but it doenst seem to be working.

    Is there an issue with what i wrote? or is this the wrong method entirely?


    Code (CSharp):
    1.  
    2.  
    3. void Walls()
    4.     {
    5.         List<GameObject> Children = new List<GameObject>();
    6.  
    7.         for (float j = 0; j< board.x; j++)
    8.         {
    9.            
    10.             for (float k = 0; k < board.y; k++)
    11.             {
    12.                 float totalPerc = (board.x * board.y * percernt) / 100;              
    13.                
    14.                 if ( Children.Count < totalPerc)
    15.                 {
    16.                     float posX = Mathf.Round(Random.Range(0f, board.x - 1f));
    17.                     float posZ = Mathf.Round(Random.Range(0f, board.y - 1f));
    18.                     Vector3 pos = new Vector3(posX, 0.5f, posZ);
    19.  
    20.  
    21.  
    22.                     Vector3 overLapScale = new Vector3(1f, 0.5f, 1f);
    23.                     Collider[] collision = new Collider[1];
    24.                     int numbColliderOverlapped = Physics.OverlapBoxNonAlloc( pos, overLapScale, collision, Quaternion.identity, 0);
    25.  
    26.                     if ( numbColliderOverlapped==0 )
    27.                     {
    28.                         GameObject wall = Instantiate(cube3, pos, Quaternion.identity);
    29.  
    30.                         wall.transform.SetParent(x.transform);
    31.                         Children.Add(wall);
    32.                     }
    33.                    
    34.  
    35.                    
    36.  
    37.                    
    38.  
    39.                    
    40.                    
    41.                 }              
    42.             }
    43.         }
    44.     }
    45. }
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,762
    Not sure if physics will work the way you contemplate above, without getting its chance to update.

    I always just store the positions in a List<Vector3> of existing positions and check all previously-placed objects to see if I'm too close. As long as the field is not SUPER dense and as long as your object count is reasonable, it will be plenty performant. If it isn't, then prove it with the profiler and look into spatial subdivisions to speed things up.

    Remember if you do a loop that Unity cannot exit, Unity WILL LOCK UP. For instance if you try to cram so many objects into an area that there simply is no possible way for more to be added, your routine will endlessly loop and your program will die. Use a master iteration counter to avoid this.
     
  3. Jinngineer

    Jinngineer

    Joined:
    Apr 22, 2020
    Posts:
    41
    Well, the plan is to make a field anywhere from 10-80% filled, which is why the percent is calculated from the board dimensions. there should always be empty space for a fresh cube.

    I will look into using a list instead of physics. Thanks for the tips :)