- Home /
how to generate random number of spawning objects outside a given point in a circle
Is there a way that you could make a random number of the same object spawn in random order around a given point. like if i had a sphere in the middle is there a way to have a random amount of cubes spawn in a circle around it every second. Im making a game where you try and protect the center by slashing objects that move toward the object in the center, but i cant figure out how to make a spawning system that will allow me to have a random amount of objects spawn in a circle around a the middle non moving object.
thanks
Just a note, clumping systems can be very, very, very hard to program, bordering on AI. You are lucky that Robert has provided beautiful code to put one random item in the area mentioned. It may be that you now face the thorny problem "what if they overlap, touch each other, touch other stuff etc" Good luck.
Answer by robertbu · Oct 02, 2013 at 03:26 AM
Here is a function that generates a random position around a central point. I've included a minDist and a maxDist so there the possibility of a bit of spread. If you want to spawn exactly on a circle, set them both to the same value or recode the line that uses them to use a fixed value. 'center' is the point you want to spawn around. You will need to call this function once for each Instantiate() to get the position.
function RandomAround (center : Vector3, minDist : float, maxDist : float) : Vector3 {
var v3 = Quaternion.AngleAxis(Random.Range(0.0, 360.0), Vector3.up) * Vector3.forward;
v3 = v3 * Random.Range(minDist, maxDist);
return center + v3;
}
it's appalling that Unity don't include "random point on circle" much as they have "within sphere" etc.
@Fattie - Yea, there are many times I would have used it.
onUnitCircle is just insideUnitCircle.normalized but it's a Vector2 in x and y.
Crap. Why can't I have insights like that?
http://docs.unity3d.com/Documentation/ScriptReference/Random.html
However dude --------- I would be concerned about how flakey that would be for very small values. (I guess too there's the corner case where you get 000, so theoretically it's no good.)
You know, i've often tried to figure the elegant way to convert onUnitSphere.html to onUnitCircle!
Hey Robert! I just realised from the docs, there's a Random.rotation - check it out.
Your answer
Follow this Question
Related Questions
Logical Error In Spawn Counter (Javascript) 2 Answers
2D Spawn with fixed random locations 0 Answers
How to Spawn a Gameobject According to a Timer? 2 Answers
Spawning Creatures 0 Answers