Hello. I would like to try out spawing enemies with Random.insideUnitCircle
var newPosition = Random.insideUnitCircle * spawnSizeArea;
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, ...
You sir, are an angel. Me thanks!
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).
Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.
The best place to ask and answer questions about development with Unity.
If you are a new user, check out our FAQ for more information.
If you are a moderator, see our Moderator Guidelines page.
We are making improvements to UA, see the list of changes.
For troubleshooting common problems with Unity 5.x Editor (including Win 10).
To help users post good questions and use the site effectively we have posted a user guide. Please check it out.
Answers and Comments
3 People are following this question.
avoid overlap problem when randomly instantiating
Instantiate Prefab at random
Spawn at random?
Instantiate Random 3D Objects/platforms that dont overlap
Instantiate multiple objects and have them raycast