Moxid
January 12, 2018, 8:42pm
1
before instantiating a prefab at a position i want to find out if there is already one in that area. i am using this method, but it doesnt work. The prefab that is instatiated has a 2d box collider and it has “is trigger”.
float xpos = Random.Range(5, -5);
float ypos = Random.Range(5, -5);
Collider[ ] hitColliders = Physics.OverlapSphere(new Vector2(xpos, ypos), 5);
if (hitColliders.Length == 1)
{
Debug.Log(“found something at that position”);
}
else
{
Instantiate(powerup1, new Vector2(xpos, ypos), transform.rotation);
}
if (hitColliders.Length > 0)
Without a mask, the OverlapSphere will return any colliders so you probably want to use a LayerMask and the other override method for OverlapSphere.
Is this a 2D game with 2D colliders? If so, you need to use Physics2D.OverlapCircle, to receive a Collider2D array.
Moxid
January 12, 2018, 8:55pm
4
Works great jeffreyschoch! i totaly forgot about the Pysics2D. Thank you so much. Here is what i got now:
float xpos = Random.Range(15, -15);
float ypos = Random.Range(15, -15);
if (Physics2D.OverlapCircle(new Vector2(xpos,ypos), 1))
{
Debug.Log(“found something at that position”);
}
else
{
Instantiate(powerup1, new Vector2(xpos, ypos), transform.rotation);
}