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

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!

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 ?