Multiple random.range areas?

Hey. This is the bit of code in my game that spawns enemies. It randomly selects a point that is withina certain radius range of the players position and instantiates an enemy there. The problem is that when the player is close to the wall that keeps the player within the playable area in the scene, the enemies will spawn on the other side of the wall (and because my game has an overhead camera, the player can see this happen). How can I make it so that the enemies will spawn at a random point around the player AND be within a set of points that are set up in world space?

Instantiate(ENEMY, transform.position +  Vector3 (Mathf.Cos(Random.Range(0, 360)), 0, Mathf.Sin(Random.Range(0, 360))) * (Random.Range(20, 50)), Quaternion.identity);}

Perhaps you can use the built-in random.insideUnitCircle function to stay inside the walls:

// Sets the position to be somewhere inside a circle
// with radius 5 and the center at zero.

var newPosition : Vector2 = Random.insideUnitCircle * 5;
transform.position.x = newPosition.x;
transform.position.y = newPosition.y;

If your maps get more elaborate and require enemies to not only not spawn outside the map, but in certain areas inside the map then you are going to need a more flexible way of controlling their spawn locations.

A sloppy way could just to place an object (i.e floor etc) that if your enemy object collides with it, it destroys that instance of the enemy object and place this object on the areas you do not want the enemy to spawn (also if this happens inside the camera view, the player will see this).

Another way is to instantiate a checker object 1st that checks whether its in the level or not (make the playable part of the level of a certain layer and check that). If returns true then it instantiates the enemy, otherwise it destroys itself.

These probably aren’t the best ways to do this, but they will probably work.

Hope this helps