Consider a Spawn Area for GameObjects. We define a Spawn Area to be a boundary consisting of 3 or more points such that an object can spawn at any coordinate that is within the boundary of the Spawn Area. Contrast this with a Spawn Point, where objects can only be spawned at that specific coordinate.
In Unity, one method of creating such a Spawn Area is to place an Empty Game Object at a point, and then use Random.Range
around that point.
Vector3 emptyGameObjectCoordinate = emptyGameObject.transform.position;
Vector3 randomSpawn = emptyGameObjectCoordinate + new Vector3(Random.Range(-1, 2), 0, 0);
My issue with this approach is that it is very difficult to visualize this area through the scene editor. It would look like a simple cube no matter what the area actually is.
I have considered possibly using a box collider as the area. This is can easily be visualized.
However, Unity Lacks a method to select points within a collider.
It would be ideal if we had a method such as Random.insideCollider
.
BoxCollider collider = GetComponent<BoxCollider>();
// Ideally. But sadly, does not exist.
Vector3 randomSpawn = Random.insideCollider(collider);
So my question is, what is the Unity way of creating a Spawn Area such that it can be visualized through the scene editor?
As a bonus, how can we create areas that consists of arbitrary points, as opposed to just being rectangles or cubes? It would still need to be possible to visualize it, and it should be easy to select random points within it.