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 AW0610AUT · Feb 05, 2014 at 09:39 PM · instantiatespawngenerationspaceasteroids

Random generation of Asteroids

Hello,

i want to generate an Astroid field.

I want to place them at random places around a coordinate with a max and min amount of asteroids or something like that. I found the instantiate command but im not sure how i would make the center var and the random range from that. And how do i make it so the "SpawnLoop" only runs as long as i want asteroids to be generated.I have 5 diffrent Asteroid Prefabs that i want to randomly choose from. And is there a way to only spawn in places that are far enough away from other asteroids?

I know these are alot of questions but it would be very helpful if someone can explain and help me with those.

Thanks in advance.

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 NickP_2 · Feb 05, 2014 at 10:31 PM 0
Share

Do you want to interact with any of the asteroids?

3 Replies

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

Answer by robertbu · Feb 05, 2014 at 10:45 PM

The only harder part of your question is "to only spawn in places that are far enough away from other asteroids". One solution is to make all of your models closely fit inside a set size sphere (unit sphere is easiest). Then you can use Physics.CheckSphere() to check to see if a position is available. Here is a bit of code to demonstrate:

 #pragma strict
  
 var asteroids : GameObject[];  // Initialized in the inspector
 private var origin = Vector3.zero;
 
 var minSize = 0.2;
 var maxSize = 1.5;
 
 var minCount = 20;
 var maxCount = 45;
 
 var minDistance = 3.0;
 var maxDistance = 15.0;
  
 function Start () {
     origin = transform.position;
     GenerateAsteroids(Random.Range(minCount, maxCount));
 }
  
 function GenerateAsteroids (count : int) {
     for (var i = 0; i < count; i++) {
         var size = Random.Range(minSize, maxSize);
         var prefab = asteroids[Random.Range(0, asteroids.Length)];
     
         for (var j = 0; j < 100; j++) {
             var pos = Random.insideUnitSphere * (minDistance + (maxDistance - minDistance) * Random.value);
             pos += origin;
             if (!Physics.CheckSphere(pos, size / 2.0)) {
                 break;
             }
         }
         var go = Instantiate(prefab, pos, Random.rotation);
         go.transform.localScale = Vector3(size, size, size);
     }
 }

In the inspector you need to initialize the 'asteroids' array with the prefabs for the asteroids.

The for() loop using 'j' repeatedly tries to place the object in a position that does not conflict using the CheckSphere() call. Note you can increase the 'radius' parameter to push things further apart as needed. Also note that it does not try forever. It tries 500 times. The reason is that certain parameters (too tight an area and/or too big of object), can result in no areas to place an object without overlapping. Without the limit on the number of tries, the app would hang. I don't know how many asteroids you will have, but it would be more efficient to keep your own list and do your own distance check rather than relying on CheckSphere(). Test your app for performance and see if you need to make the change.

Comment
Add comment · Show 7 · 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 AW0610AUT · Feb 06, 2014 at 04:42 PM 0
Share

The script works great i just tried it. Thanks. I dont have time today to look at the code but on the weekend i will look into it and try to understand it. Anyway thanks for the help.

avatar image AW0610AUT · Feb 06, 2014 at 07:11 PM 0
Share

Ok just a one more thing. I attached the script to an empty game object and i want to set the origin var to the game objects transform.postion but when i change it to var origin = transform.postion; it doesnt work. It still takes the input that is set in the inspector view. I checked that by adding a Debug.Log that displays the origin.

avatar image robertbu · Feb 06, 2014 at 08:04 PM 1
Share

Your likely problem is you did this in Start():

  var origin = transform.postion;

Ins$$anonymous$$d you need to do this:

  origin = transform.postion;

Because you use 'var', you created a local variable that hides the instance variable at the top of the file.

avatar image AW0610AUT · Feb 07, 2014 at 05:48 PM 0
Share

I have done it the way you said but it still spawns around (0,0,0) even though the Debug.Log now shows the loc of the empty game object.

avatar image robertbu · Feb 07, 2014 at 06:03 PM 0
Share

I just tested it, and it worked fine. Without seeing your code, I don't know where you have the issue. I edited the above script to what I tested. It now takes the 'origin' from the position of the game object the script is attached to.

Show more comments
avatar image
1

Answer by morgan23 · Feb 05, 2014 at 10:46 PM

You can find something like that in a tutorial series on the website under learn /tutorials/ projects/project space shooter it's very good too for understanding basics.

Comment
Add comment · 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
0

Answer by Snownebula · Jul 30, 2014 at 11:29 PM

Anyone else getting there asteroids floating away? Is there a way to prevent them from just floating too far away?

Comment
Add comment · Show 2 · 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 AW0610AUT · Jul 31, 2014 at 12:56 AM 0
Share

They are probably floating away because their spawns intersect each other and they get pushed. Or do you mean because you are pushing them?

You could write a script that checks how far they are away from your worlds origin. Something like this (untested/pseudo-code):

     var x = transform.position.x
     var y = transform.position.y
     var z = transform.position.z
     
     var maxDistance = 100.0;
     
     function Update (){
     if (x>maxDistance || y>maxDistance ||z>maxDistance)
     rigidbody.velocity = Vector3(0,0,0);
     
     }

This would basically freeze the asteroid if it gets to far away from the origin.

avatar image Snownebula · Jul 31, 2014 at 12:53 PM 0
Share

I think its because there spawns intersect each other on start.

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

23 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 avatar image

Related Questions

Spawning if there is space 1 Answer

How can I go about creating an "Infinate" asteroid field?? 1 Answer

Spawning enemies at an offset 1 Answer

Create a random group of asteroids. 2 Answers

How to to spawn player, according to id? 0 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