Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Physics2D.OverlapCircle problems

Discussion in 'Scripting' started by Mothil, Apr 25, 2015.

  1. Mothil

    Mothil

    Joined:
    Jan 14, 2014
    Posts:
    434
    Code (CSharp):
    1.     private bool colliding;
    2.     public Transform freeSpace;
    3.     public float radius = 10;
    4.     public LayerMask freeRun;
    5.  
    6.     //Collider2D colliders = Physics2D.OverlapCircle(prefab.point, radius);
    7.  
    8.     void Start()
    9.     {
    10.         Spawner ();
    11.        
    12.         colliding = Physics2D.OverlapCircle (prefab.transform.position, radius, freeRun);
    13.     }
    14.    
    15.     void Spawner()
    16.     {
    17.         for (int i = 0; i < numObjects; i++)
    18.         {
    19.             if(colliding == !true)
    20.             {
    21.                 Vector2 pos = Random.insideUnitCircle * 4;
    22.                 Instantiate (prefab, pos, transform.rotation);
    23.             }
    24.         }
    25.     }
    I am unsure about how I can make my objects only spawn if there is free space. I think I'm fairly close with this script, but there are certain things that just doesn't make sense here. How can I make them only spawn if they are not colliding?
     
  2. Mothil

    Mothil

    Joined:
    Jan 14, 2014
    Posts:
    434
    I now have wire gizmos showing a radius. I feel like there should be a really easy way of making them only spawn if they are outside that radius! I just don't know how.

    wiregizmos.jpg
     
  3. Mothil

    Mothil

    Joined:
    Jan 14, 2014
    Posts:
    434
    Ahh, this is killing me. It doesn't feel like it should be too hard to solve...
    I'm thinking something like this:

    Pseudo code:
    calculate random position,
    if not colliding with other objects radius -> spawn
    if colliding with other objects radius -> recalculate (find new spawn position)
    if no room to spawn, don't spawn
     
  4. RiokuTheSlayer

    RiokuTheSlayer

    Joined:
    Aug 22, 2013
    Posts:
    356
    Code (CSharp):
    1.     void SpawnObject(){
    2.         bool place = true;
    3.         Vector2 check;
    4.  
    5.         int tries = 4;
    6.  
    7.         //This will loop four times, and try to find a collision each time until it finds a valid postion or runs out of attempts.
    8.         while(place){
    9.  
    10.             if(tries<=0) break;
    11.  
    12.             //Get a new random position
    13.             check = Random.insideUnitCircle*4;
    14.             //Check that position for collision.
    15.             place=!Physics2D.OverlapCircle (prefab.transform.position, radius, freeRun);
    16.  
    17.             tries--;
    18.         }
    19.  
    20.         if(!place){
    21.             Instantiate (prefab, pos, transform.rotation);
    22.         }
    23.  
    24.     }
    Something like this. This will try 4 times to find a place that's free, and if it does, it'll spawn an object there. If it finds no spots, it'll just not spawn an object. If you'd like, turn tries up to something like 300. Just don't let it loop forever, because if for some reason there's no available spots nearby, it will crash your game due to an infinite loop.
     
    Mothil likes this.
  5. RiokuTheSlayer

    RiokuTheSlayer

    Joined:
    Aug 22, 2013
    Posts:
    356
    It may actually be better to check in the loop if the place is available, and if it is, spawn an object there then break the loop, but still.
     
  6. Mothil

    Mothil

    Joined:
    Jan 14, 2014
    Posts:
    434
    I was thinking about maybe trying to spawn them in a grid instead, and tweak the positions as they are spawned. In that way it doesn't need to check if places are open, as they are placed directly on X/Y: 1,1 - 1,2 - 1,3 and so forth, and stopping at a certain length.

    randomsmallamounts.png

    let's say that one in the middle spawned objects like those at the top, inside the green circle, and randomly adjusted their position with something between -0.3 and +0.3, and when the player walks, the ones falling outside the green circle are despawned and placed at the new open positions.

    Would this be easier to implement? It certainly sounds like it. I have no idea how, but at least I feel like I at least have a shot at this now. :)