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

kidshenlong gravatar image

5 36 26 27

(comments are locked)
10|3000 characters needed characters left

1 answer: 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

syclamoth gravatar image

18.3k 19 26 107

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
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: 2324 times

Last Updated: Feb 26, 2012 at 03:50 AM