Hi,
I want to prevent my enemy to spawn on my player. I have tried to do it with a while loop, but it always freeze the game. Could someone help me
Here is my code:
public GameObject Enemy;
public GameObject Player;
public void Start () {
//Create a new enemy.
Instantiate(Enemy, SpawnPosition(), Quaternion.identity);
}
//Create enemy position.
Vector2 SpawnPosition(){
float x, y;
x = Random.Range(-2.7f, 2.7f);
y = Random.Range(3.8f, -4.0f);
//Create new place to spawn outside player.
while(Vector2.Distance(Player.transform.position,new Vector2(x, y)) < 7 )
{
x = Random.Range(-2.7f, 2.7f);
y = Random.Range(3.8f, -4.0f);
}
return new Vector2(x, y);
}