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!

more ▼

asked Jun 21, 2011 at 05:03 AM

superventure gravatar image

760 158 134 161

(comments are locked)
10|3000 characters needed characters left

2 answers: sort voted first

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, ...
more ▼

answered Jun 21, 2011 at 05:24 AM

aldonaletto gravatar image

57.6k 33 91 301

You sir, are an angel. Me thanks!

Jun 21, 2011 at 05:30 AM superventure

Oops! I wrote xz.z, but the it should be xz.y! Edited the answer...

Jun 21, 2011 at 05:46 AM aldonaletto
(comments are locked)
10|3000 characters needed characters left

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).

more ▼

answered Jun 21, 2011 at 05:17 AM

Dreamblur gravatar image

774 32 34 46

(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here



Answers and Comments



asked: Jun 21, 2011 at 05:03 AM

Seen: 2613 times

Last Updated: Jun 21, 2011 at 05:46 AM