- Home /
Why does inside unit circle use the world point?
Hello. I would like to try out spawing enemies with Random.insideUnitCircle
var newPosition = Random.insideUnitCircle * spawnSizeArea;
yield WaitForSeconds(Random.Range(5,12));
Instantiate(curSpawn, newPosition, transform.rotation);
but this is seemingly using the world point instead of the radius around the gameobject I attached this script to. How do I undo that? Thank you!
Answer by aldonaletto · Jun 21, 2011 at 05:24 AM
Random.insideUnitCrcle returns a Vector2. You should convert it to Vector3 and translate to the game object position:
var xz = Random.insideUnitCircle * spawnSizeArea;
var newPosition = Vector3(xz.x,0,xz.y)+transform.position;
Instantiate(curSpawn, newPosition, ...
Oops! I wrote xz.z, but the it should be xz.y! Edited the answer...
Answer by Dreamblur · Jun 21, 2011 at 05:17 AM
Random.insideUnitCircle returns a Vector2. When a Vector2 is implicitly converted to a Vector3, the z-component is set to 0. For example, let's assume that spawnSizeArea = 5 and Random.insideUnitCircle returns Vector2(0, 1). When you try to use that as the spawn position of your new instance, it'll spawn at the point (0, 1, 0).
Now, if your problem is that it's not spawning the instance at a local point relative to your game object, well, it's not supposed to. If you want to do that, you have to manually convert the point to local space coordinates by using InverseTransformDirection or InverseTransformPoint (whichever works for your purposes).