Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by Rxanadu · Apr 14, 2014 at 11:51 PM · triggerrandomradius

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?

Comment
Add comment · Show 4
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Benproductions1 · Apr 15, 2014 at 03:52 AM 0
Share

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.

avatar image robertbu · Apr 15, 2014 at 04:26 AM 1
Share

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

avatar image FireFrecklez · Jun 24, 2019 at 05:06 AM 0
Share

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.

avatar image centaurianmudpig FireFrecklez · Jun 30, 2019 at 11:54 AM 0
Share

Add the position of your "area spawner" to your random position.

1 Reply

· Add your reply
  • Sort: 
avatar image
0
Best Answer

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.

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Benproductions1 · Apr 15, 2014 at 11:06 PM 1
Share

^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

Hint: You can notify a user about this post by typing @username

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

Answers Answers and Comments

22 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges