Hi
For our game we have a collectible spawning system where the collectibles spawn in a maze, and the area they spawn in gets smaller, forcing players into the center of the maze. One issue I am having is when I generate the random point for the collectible to spawn, sometimes it spawns under a wall. When i generate the point the collectible is about to spawn at, is there any way i can check if that point, or a Vector2 is overlapping with a particular layer, sorting layer of box colliders. I essentially want the code to be able to re pick the random point if it sees the gem would be under a wall. The gem doesn’t have its own collider or rigid body, as it is underground, and the player has to dig on top of it. Here is the code for spawning the gems.
public GameObject Gem;
public int initialNumberOfGems = 10;
Vector2 spawnLocation;
private void OnEnable()
{
GemScript.onGemDug += spawnNewGem;
}
private void OnDisable()
{
GemScript.onGemDug -= spawnNewGem;
}
public static float height;
public static float width;
private void Start()
{
height = Camera.main.orthographicSize - 2f;
width = (height * Camera.main.aspect) - 2f;
for (int i = 0; i < initialNumberOfGems; i++)
{
Instantiate(Gem, getSpawnLocation(), Quaternion.identity);
}
}
private void spawnNewGem(bool isRed, Vector2 gemPos, int value){
if(height >= 3f){
height -= 0.5f;
}
if(width >= 3f){
width -= 0.5f;
}
Instantiate(Gem, getSpawnLocation(), Quaternion.identity);
}
private Vector2 getSpawnLocation(){
float RandomX = UnityEngine.Random.Range(-width, width);
float RandomY = UnityEngine.Random.Range(-height, height);
Vector2 point = new Vector2(RandomX,RandomY);
return point;
}