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 tyguy · Oct 02, 2013 at 02:42 AM ·

how to generate random number of spawning objects outside a given point in a circle

Is there a way that you could make a random number of the same object spawn in random order around a given point. like if i had a sphere in the middle is there a way to have a random amount of cubes spawn in a circle around it every second. Im making a game where you try and protect the center by slashing objects that move toward the object in the center, but i cant figure out how to make a spawning system that will allow me to have a random amount of objects spawn in a circle around a the middle non moving object.

thanks

Comment
Add comment · Show 1
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 Fattie · Oct 02, 2013 at 08:35 AM 0
Share

Just a note, clumping systems can be very, very, very hard to program, bordering on AI. You are lucky that Robert has provided beautiful code to put one random item in the area mentioned. It may be that you now face the thorny problem "what if they overlap, touch each other, touch other stuff etc" Good luck.

1 Reply

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

Answer by robertbu · Oct 02, 2013 at 03:26 AM

Here is a function that generates a random position around a central point. I've included a minDist and a maxDist so there the possibility of a bit of spread. If you want to spawn exactly on a circle, set them both to the same value or recode the line that uses them to use a fixed value. 'center' is the point you want to spawn around. You will need to call this function once for each Instantiate() to get the position.

 function RandomAround (center : Vector3, minDist : float, maxDist : float) : Vector3 {
     var v3 = Quaternion.AngleAxis(Random.Range(0.0, 360.0), Vector3.up) * Vector3.forward;
     v3 = v3 * Random.Range(minDist, maxDist);
     return center + v3; 
 }
Comment
Add comment · Show 9 · 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 Fattie · Oct 02, 2013 at 08:35 AM 0
Share

it's appalling that Unity don't include "random point on circle" much as they have "within sphere" etc.

avatar image robertbu · Oct 02, 2013 at 04:32 PM 0
Share

@Fattie - Yea, there are many times I would have used it.

avatar image Fattie · Oct 02, 2013 at 04:47 PM 0
Share

your code there is lovely by the way

avatar image whydoidoit · Oct 02, 2013 at 04:55 PM 0
Share

onUnitCircle is just insideUnitCircle.normalized but it's a Vector2 in x and y.

avatar image Fattie · Oct 02, 2013 at 05:01 PM 0
Share

Crap. Why can't I have insights like that?

http://docs.unity3d.com/Documentation/ScriptReference/Random.html

However dude --------- I would be concerned about how flakey that would be for very small values. (I guess too there's the corner case where you get 000, so theoretically it's no good.)

You know, i've often tried to figure the elegant way to convert onUnitSphere.html to onUnitCircle!

Hey Robert! I just realised from the docs, there's a Random.rotation - check it out.

Show more comments

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

16 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

Related Questions

Logical Error In Spawn Counter (Javascript) 2 Answers

2D Spawn with fixed random locations 0 Answers

How to Spawn a Gameobject According to a Timer? 2 Answers

Spawning Creatures 0 Answers

Instantiate stop when all of them have destroyed 1 Answer


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