Answers for "Spawn at random?"
http://answers.unity.com/questions/34435/spawn-at-random.html
The latest answers for the question "Spawn at random?"Answer by skovacs1
http://answers.unity.com/answers/34531/view.html
<p>You would need to get your room dimensions somehow (I assume that's what you mean by "if I set boundaries"), then you can use a random value for your instantiate position.</p>
<p>Instantiate in a circle:</p>
<pre><code>//Assumes you know the radius of the area and the height at which to instantiate
var pos : Vector2 = Random.insideUnitCircle * radius;
Instantiate(prefab, Vector3(pos.x,height,pos.y),
Quaternion.LookRotation(center));
</code></pre>
<p>or inside a sphere:</p>
<pre><code>//Assumes you know the radius of the area
Instantiate(prefab, Random.insideUnitSphere * radius,
Quaternion.LookRotation(center));
</code></pre>
<p>or in a box:</p>
<pre><code>//Assumes Vector3's for the minimum and maximum positions
Instantiate(prefab, Vector3(Random.Range(minimum.x,maximum.x),
Random.Range(minimum.y,maximum.y),
Random.Range(minimum.z,maximum.z)),
Quaternion.LookRotation(center));
</code></pre>
<p>For arbitrarily shaped areas, you would have to write something more complicated. The brute force solution is to keep generating points until you get one inside the volume. </p>
<p>Another more storage-intensive approach (at least when doing the calculation of the subdivisions) is to subdivide the area into equal and uniform divisions and then pick one at random to position yourself in and, since the area is uniform, from there, it would be as the simple code above.</p>
<p>Anything else becomes much harder, especially if you cannot guarantee the area is convex.</p>Thu, 18 Nov 2010 15:20:01 GMTskovacs1