- Home /
Making spawn area using Random.insideUnitSphere on x- and z- axes
I'm making a wave-based survival game where the enemies will spawn from the sky. In order to do this, I placed a sphere trigger collider above the arena in the game so it looks like they're falling down from the clouds. Currently, I'm using Random.insideUnitSphere * (sphere collider's radius) to spawn the enemies in a random spot every time. However, I've noticed some of the enemies spawn below the arena even though the trigger is well above it.
The intended result of my efforts is to have the enemies spawn at the same height to later descent calculate their distance from the ground, allowing them to move around freely. Is there any way to do this using Random.insideUnitSphere, or would I have to use something else?
You could just take the x and z components of your random position and ignore the y component, giving your own one. Or you could make your own randomInsideCircle
function.
@Benproductions1 - I think the distribution with be center heavy if 'y' is ignored.
@Rxanadu - Just use random.insideUnitCircle and replace 'z' with 'y' in the result and then set 'y' to your height.
Hey guys I was wondering how you managed to get the spawner to set the spawn location based on the object you attached the script too? my enemies keep spawning within a radius of the center of my scene rather then the game object the script is attached too. ,Hey guys I was just wondering how you managed to get the spawner to spawn based on the location of your gameobject which this script is attached too? I have used the code ( thank you) for your random spawner but it seems as though it doesn't matter where I place my "area spawner" the enemies always spawn in the dead center of my map.
Add the position of your "area spawner" to your random position.
Answer by Rxanadu · Apr 15, 2014 at 09:17 PM
@robertbu: I did something similar to that where I used the x- and z- axes for Random.insideUnitSphere within a new Vector3 object, and it worked.
Specifically, the code for my solution is the following:
spawnPosition = new Vector3(Random.insideUnitSphere.x * spawnTrigger.radius,
transform.position.y, Random.insideUnitSphere.z * spawnTrigger.radius);
Here, 'spawnPosition' is a Vector3 object to be referenced later in the program; whereas, the 'spawnTrigger' is referencing a SphereCollider component I have attached to the game object this script will be placed on.
The SphereCollider object is used to reference its radius' x- and z- axis values, as I need them to "spread out" the potential spawn position. Otherwise, the enemy will spawn extremely close to the center of the spawn area.
^That is virtually identical to:
new Vector3((Random.value*2 - 1) * spawnTrigger.radius,
transform.position.y,
(Random.value*2 - 1) * spawnTrigger.radius);
And is not a random position inside of a circle, but inside of a square.
Your answer
Follow this Question
Related Questions
Why can't void be used in this context? 2 Answers
Help with destroying within a certain zone... 1 Answer
triggering random animations with gui 1 Answer
Randomly spawn a trigger area 1 Answer
Spawn Random Item On Trigger Enter 2 Answers