How to randomly spawn position of object within a area ?
Here’s a quick function to get a random point inside a box using the same inputs that Unity uses to draw boxes (e.g. Gizmos.DrawWireCube):
private static Vector3 RandomPointInBox(Vector3 center, Vector3 size) {
return center + new Vector3(
(Random.value - 0.5f) * size.x,
(Random.value - 0.5f) * size.y,
(Random.value - 0.5f) * size.z
);
}
Well it isn’t hard to get the dimension of an object. so find min & max of each layer of dimension you are using.
Then just do
Instantiate(gameObject, Vector3(Random.Range(minY,maxY), Random.Range(minZ,maxZ), Random.Range(minX,maxX)), Quaternion.identity)