# Spawn Object Outside of Radius

Hey Guys,

I was wondering how would I spawn an enemy object outside of a player objects radius?

For example, I have enemy ship objects that spawn at random positions onto the play field. What I would like to do, is create a radius (let’s say of 5) around the player so when a ship spawns, it doesn’t spawn on top or next to the player. This also gives a chance for the player to react to an enemy spawn.

I’m not sure how I would go about this, so I don’t have code for it. At the moment this is the code for my spawner:

IEnumerator SpawnWaves()
{
yield return new WaitForSeconds(startWait);
while (true) {
for (int i = 0; i < hazrdCount; i++)
{
GameObject hazard = hazards[Random.Range (0, hazards.Length)] ;
Vector3 spawnPosition = new Vector3(Random.Range(-spawnValues.x, spawnValues.x), spawnValues.y, Random.Range(-spawnValues.z, spawnValues.z));
Quaternion spawnRotation = Quaternion.identity;
Instantiate(hazard, spawnPosition, spawnRotation);
yield return new WaitForSeconds(spawnWait);
}
yield return new WaitForSeconds(waveWait);
}
}

Right now the enemy objects don’t spawn outside of the world, which is good. But I would like to make sure no matter where the player object is on the map, enemy objects won’t spawn relative to the player object of a given radius.

Thanks,
Mark.D

This will make sure enemy ship will spawn at a distance between 5.0 to 20.0.

float minDistance = 5.0;
float maxDistance = 20.0;
float distance    = Random.Range( minDistance, maxDistance );
float angle       = Random.Range( -Mathf.PI, Mathf.PI );

Vector3 spawnPosition = playerTransform.position ;
spawnPosition += new Vector3( Mathf.Cos( angle ), 0, Mathf.Sin( angle ) ) * distance;

spawnPosition.x = Mathf.Clamp( spawnPosition.x, -spawnValues.x, spawnValues.x );
spawnPosition.y = spawnValues.y ;
spawnPosition.z = Mathf.Clamp( spawnPosition.z, -spawnValues.z, spawnValues.z );

Good day.

As you randomize the spawn point, you can just check the distance from the spawnpoint to the player, and if is too close, recalculate the spawnpoint, like this:

GameObject hazard = hazards[Random.Range (0, hazards.Length)] ;
Vector3 spawnPosition = new Vector3(Random.Range(-spawnValues.x, spawnValues.x), spawnValues.y, Random.Range(-spawnValues.z, spawnValues.z));
while (Vector3.distance(spawnPosition, player.transform.position) < 50)
{
spawnPosition = new Vector3(Random.Range(-spawnValues.x, spawnValues.x), spawnValues.y, Random.Range(-spawnValues.z, spawnValues.z));
}
Quaternion spawnRotation = Quaternion.identity;
Instantiate(hazard, spawnPosition, spawnRotation);

Now, it will be doing the While sentence remaking the spawnpoint position until the distance is more than 50.

Be aware to check the while condition before play the game in Editor, because if while never become false, it willstay isidie the while “forever” and Unity will stay blocked, you you will need to Ctl+Alt+Sup to stop Unity … so save the project every time until you are sure the while works perfect.

Bye!

This will return a random position that’s 20 to 50 meters away:

Vector3 pos=new Vector3(Random.value-0.5f,0,Random.value-0.5f).normalized*Random.Range(20,50);