Search Unity

  1. Unity 2019.2 is now released.
    Dismiss Notice

How to prevent spawn overlap?

Discussion in '2D' started by udittchoudhury, Jul 15, 2019.

  1. udittchoudhury

    udittchoudhury

    Joined:
    Feb 5, 2019
    Posts:
    1
    I'm trying to make a snake like game where the player eats the "FOOD" and it spawns at some random point within the scene. I'v added just one complication that is there are obstacles(Boxes) in the scene, and the player has to maneuver without colliding in those boxes and eat the food. Everything else is working fine; My only problem is the "FOOD" sometimes spawn on top of the boxes. I want to prevent that.

    I've followed this YouTube tutorial where the guy uses
    Physics2D.OverlapCircleAll()
    function.

    It works to an extent, that is the center of the food never spawns on top if the box, but the edges still does (just like in the tutorial). The tutorial never solves this issue. Can someone help me?

    Note: I'm not using grids to move the player like other snake games.

    Here's my code for randomly spawning the food:

    Code (CSharp):
    1. using UnityEngine;
    2.  
    3.  
    4. public class FoodSpawner2 : MonoBehaviour
    5. {
    6.     public static FoodSpawner2 instance;
    7.     public float xbound;
    8.     public float ybound;
    9.     public GameObject foodPrefab;
    10.     public GameObject currentFood;
    11.  
    12.     public float radius;
    13.     public Collider2D[] collidersss;
    14.     //Spawns the food for the first time
    15.     void Start()
    16.     {
    17.         FoodLoaction();
    18.     }
    19.  
    20.     private void Update()
    21.     {
    22.         //Spawns the food when it gets eaten by the snake
    23.         if (currentFood == null)
    24.         {
    25.             FoodLoaction();
    26.         }
    27.         //FoodLoaction();
    28.     }
    29.  
    30.     public void FoodLoaction()
    31.     {
    32.         Vector2 SpawnPos = new Vector2();
    33.         bool canSpawnHere = false;
    34.         int safetynet = 0;
    35.  
    36.         while(!canSpawnHere)
    37.         {
    38.             float xPos = Random.Range(-xbound, xbound);
    39.             float yPos = Random.Range(-ybound, ybound);
    40.  
    41.             SpawnPos = new Vector2(xPos, yPos);
    42.             canSpawnHere = PreventSpawnOverlap(SpawnPos);
    43.  
    44.             if(canSpawnHere)
    45.             {
    46.                 break;
    47.             }
    48.  
    49.             safetynet++;
    50.  
    51.             if(safetynet > 50)
    52.             {
    53.                 break;
    54.                 //Debug.Log("toooooooo");
    55.             }
    56.         }
    57.  
    58.         currentFood = Instantiate(foodPrefab, SpawnPos, Quaternion.identity) as GameObject;
    59.  
    60.     }
    61.  
    62.     public bool PreventSpawnOverlap(Vector2 spawnPos)
    63.     {
    64.         collidersss = Physics2D.OverlapCircleAll(transform.position, radius);
    65.         for(int i = 0; i< collidersss.Length; i++)
    66.         {
    67.             Vector2 centerpoint = collidersss[i].bounds.center;
    68.             float width = collidersss[i].bounds.extents.x;
    69.             float height = collidersss[i].bounds.extents.y;
    70.  
    71.             float leftExtent = centerpoint.x - width;
    72.             float rightExtent = centerpoint.x + width;
    73.             float lowerExtent = centerpoint.y - height;
    74.             float upperExtent = centerpoint.y + height;
    75.  
    76.             if(spawnPos.x>=leftExtent && spawnPos.x <= rightExtent)
    77.             {
    78.                 if(spawnPos.y >= lowerExtent && spawnPos.y <= upperExtent)
    79.                 {
    80.                     return false;
    81.                 }
    82.             }
    83.         }
    84.         return true;
    85.     }
    86.  
    87. }
    88.