Before updating to Windows 10 you may need to return your Pro license. More information here

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 :)

more ▼

asked Feb 19, 2012 at 01:16 AM

avatar image

15 36 27 29

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 !


Mar 27 at 06:37 AM Ego65

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.

Mar 27 at 07:21 AM craigb

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

Mar 27 at 09:53 AM Ego65

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. Make sure that the LOD3 is a billboard that is always facing the camera.

Mar 28 at 06:10 PM craigb
(comments are locked)
10|3000 characters needed characters left

2 answers: sort voted first

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!

more ▼

answered Feb 19, 2012 at 01:29 AM

avatar image

19.4k 24 46 157

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 mind explain how to set it up a little more please??

Thank you :)

Feb 20, 2012 at 11:01 PM kidshenlong

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.

Feb 21, 2012 at 08:40 AM syclamoth

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??

Feb 21, 2012 at 05:12 PM kidshenlong

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?

Feb 23, 2012 at 03:48 AM syclamoth

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

Feb 23, 2012 at 01:16 PM kidshenlong
(comments are locked)
10|3000 characters needed characters left

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 ?

more ▼

answered May 09 at 06:30 AM

avatar image


(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

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

By Email:

Once you sign in you will be able to subscribe for any updates here



Answers and Comments



asked: Feb 19, 2012 at 01:16 AM

Seen: 3011 times

Last Updated: May 09 at 06:47 AM