Question by
robbutch01 · Jul 10, 2020 at 07:24 PM ·
c#physics2dspawning
Trying to spawn a prefab and reposition if another prefab is detected using physics2d.
Aim:
A spawned enemy prefab checks to see if it is overlapping with another enemy prefab and if so repositions itself.
There is only 1 prefab which is cloned over every X Seconds.
Code:
Collider2D[] hits; = Physics2D.OverlapCircleAll(transform.position, 1f);
foreach (Collider2D hit in hits)
if (hit.tag == "Enemy" )
{
float randomX = Random.Range(-2.2f, 2.2f);
float randomY = Random.Range(7.5f, 11.5f);
transform.position = new Vector3(randomX, randomY, 0f);
}
Problem
I think the Prefab is detecting itself and respositioning which I obviously dont want. I only want to reposition if it detects another copy of the prefab.
Any help would be appreciated.
Comment