• 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 kidshenlong · Feb 19, 2012 at 01:16 AM · objectloops

Creating an Asteroid field from Scratch?

So I'm working on an on rails space shooter and I'm trying to figure out how to add in an asteroid field.

I'm not too sure where to start. I mean I know that adding loads of asteroids in would be a bad idea. So I was hoping to come up with one and then instantiate(?) it through script into different places and sizes and even add rotation and movement if possible.

Like I said I'm a bit unsure of how to start this so any help would be really handy.

Thanks guys :)

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 Ego65 · Mar 27, 2015 at 06:37 AM 0
Share

Hi, thx for this. it works so far but the asteroids stopping soon to rotate after being instantiated ! I'm also a beginner and i tried much to rotate them permanently but i havent achiefed it. I guess somehow in the update function. But how to change the script to make the asteroids rotate permanently ? Although this topic is 3 years old i hope i found some help thx previously !

yours

avatar image craigb · Mar 27, 2015 at 07:21 AM 0
Share

Ego65 you just take any drag off the rigidbody. my asteroids rotate for ever and I have 0 Drag and 0 Angular Drag on the Rigidbody. BTW Do this on the prefab.

avatar image Ego65 · Mar 27, 2015 at 09:53 AM 0
Share

thx very much ! works perfectly now :) i would like to have different asteroids. what would be a "clean" way to achiefe this ?

Either : create diff ast-prefabs and instantiate them in this script like:

GameObject newAsteroid01=(GameObject)Instantiate(ast-Prefab01... GameObject newAsteroid02=(GameObject)Instantiate(ast-Prefab02...

Or : Attach this script to each kind of asteroid ?

I think this could be a thing which will influence the calculating perfomance of Unity

avatar image craigb · Mar 28, 2015 at 06:10 PM 0
Share

Yeah I just placed it on another prefab but this isn't the best way because you get ones overlapping I tried adding colliders to them but this got hilarious effects of them colliding voilently as the intiantate into the world and flinging off into space. If you create a way to put variations into one instantate let us know because I would love to use it. Btw the performance shouldn't be an issue if you use the LOD system. $$anonymous$$ake sure that the LOD3 is a billboard that is always facing the camera.

2 Replies

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

Answer by syclamoth · Feb 19, 2012 at 01:29 AM

Well, given a simple sphere prefab, you could try something like this-

 for(int i = 0; i < 100; ++i)
 {
     GameObject newAsteroid = (GameObject)Instantiate(asteroidPrefab, Random.insideUnitSphere * fieldRadius, Random.rotation);
     float size = Random.Range(0.3f, 2);
     newAsteroid.transform.localScale = Vector3.one * size;
     // if the asteroid has a rigidbody...
     newAsteroid.rigidbody.velocity = Random.insideUnitSphere * movementSpeed;
     newAsteroid.rigidbody.angularVelocity = Random.insideUnitSphere * rotationSpeed;
 }

This should spit out a bunch of asteroids for you!

Comment
Add comment · Show 6 · 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 kidshenlong · Feb 20, 2012 at 11:01 PM 0
Share

That seems like it'd work but I'm a bit unsure on how to set it up. I'm quite the noob to unity. I've created a prefab called Asteroid, and I've got a script file called asteroid field. Would you $$anonymous$$d explain how to set it up a little more please??

Thank you :)

avatar image syclamoth · Feb 21, 2012 at 08:40 AM 0
Share

Well, obviously I haven't provided you with an entire script. I'll give you snippets like this, but I'm not that keen on people copy-pasting my code without understanding it. For starters, make a C# script file (because that's what languge this is in), and give it the public variables:

 public GameObject asteroidPrefab;
 public float fieldRadius;
 public float size;
 public float movementSpeed;
 public float rotationSpeed;

This will allow you to configure it in the inspector. If you want to use the 'movement' parts, you'll also have to give your asteroids rigidbody components. If it's set in space, you should also disable gravity on them.

avatar image kidshenlong · Feb 21, 2012 at 05:12 PM 0
Share

Thanks, for your help. I'm pretty sure I get the code, It's just tweaking it to do exactly what I need it to which is the challenging part...

In terms of placement of the Asteroid, I tried messing around with the placement

"GameObject newAsteroid = (GameObject)Instantiate(asteroidPrefab, Vector3(Random.Range(500,1500), Random.Range(-100, 200), Random.Range(50, 500)), Random.rotation);" but this throws up an error saying "Expression denotes a type, where a variable, value or method group is expected. Not too sure what that means... but I guess you can kinda see what I was trying to do with that code...

Also I was unsure where exactly to place the code that creates the asteroid. I've placed it in the start function and the update but they both cause keep making loads of asteroids and slow down unity til it crashes...

Any ideas on what I'm doing wrong??

avatar image syclamoth · Feb 23, 2012 at 03:48 AM 0
Share

You need to have the 'new' keyword before 'Vector3(value value value)'. There's a reason why I was using Random.insideUnitSphere- unless you want them spawning inside a box, not a ball. Don't put it in update, it'll just keep spawning asteroids. Put it in start- the entire point of the

 for(int i = 0; i < 100; ++i)

line is that it executes that code exactly 100 times.

Do you know what the difference between a type and a value is?

avatar image kidshenlong · Feb 23, 2012 at 01:16 PM 0
Share

Sorted it now! I think I was thinking into it too much lol

I'm not entirely sure of the difference, I'm a little rubbish with the actually words for coding stuff lol

Show more comments
avatar image
0

Answer by Ego65 · May 09, 2015 at 06:30 AM

thx craigb ! got it !

now that i wanted to use it in a new scene i realized that i can't set the ast.-field at a wanted position. seems it always spreads the asteroids from 0,0,0. i attached this script to empty game object and placed it at 0,0,700. so i thought it would spit out the ast. from this point. but it doesn't ! i tried to work with a public transform and instantiate the prefab there but whatever i try i get errors.

would someone help me on how to set the ast.-field to the coords of the empty game object please ?

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

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

8 People are following this question.

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

Related Questions

On mouse Enter 1 Answer

Controlling visibility of several objects with GUI 2 Answers

Array - Convert Object into Int 5 Answers

Stop Rigidbodies From Overlapping 4 Answers

arc object's position using sin 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