Hello everyone, I’m trying to create an infinite asteroid field that generates asteroids as I move through the level.
I managed to instantiate a certain number of asteroids at Start, but of course, if I move too far away nothing else gets generated.
I don’t exactly know how to do this, I was thinking about using a Perlin noise but I wouldn’t know how to implement it.
I do have a list of models that I can use, so there’s no need to generate the asteroid mesh as well.
Can anyone help me with this?
The basic strategy for infinite worlds is to keep picking objects off one side as the screen as they disappear and dropping them back on the other.
Does the field need to be persistiant? In other words, if the player does a u turn, will they notice if the asteroid feild has changed?
If you want persistence you will need to use some sort of pseudo random noise. Fractal noise may be more appropriate then Perlin.
If you don’t need persistence it’s simply a case of picking a random location and including enough of a dead band to keep the player from wising up.
Well it is a 3d game so persistency could be an issue, altough considering that the asteroids will move around/rotate, having an asteroid disappear or another asteroid appearing out of nowhere shouldn’t look wrong.
About the generation, I have an array of gameobjects that contains all the possible meshes for the asteroids, then I have a float indicating a radius and an asteroid count number. What I did on start was this:
void Start () {
for (int i = 0; i < numberOfAsteroids; i++)
Instantiate (asteroids[Random.Range (0, asteroids.Length)], transform.position + Random.insideUnitSphere * radius, Random.rotation);
}
I guess I’ll have to use the Update function for this one, but how could I implement it?
Put a massive trigger collider surrounding your player. As asteroids exit the trigger, move them to a random position on the opposite side of the collider.
I added a sphere trigger in runtime the same radius as the Random.insideUnitSphere’s radius, then I attached this script to every asteroid’s prefab:
void OnTriggerExit () {
float radius = //Get the radius from the other script;
Vector3 newSphereCenter = GameObject.FindGameObjectWithTag ("Player").transform.position + GameObject.FindGameObjectWithTag ("Player").transform.forward * radius;
Vector3 nextPos;
do {
nextPos = newSphereCenter + Random.insideUnitSphere * radius;
} while (Vector3.Distance (nextPos, GameObject.FindGameObjectWithTag ("Player").transform.position) < radius);
transform.position = nextPos;
}
And it seems to be working, do you think some parts can be improved or is it good to go?
Generally if it works it’s good to go but for your own interest you might check out using a particle system to simulate asteroids that might save you some time. In the past I’ve used the Legacy particle system to take care of a lot of the instantiate, remove and apply random features to individual asteroids which requires minimal code to get up and running.
You can check out a decent example at : http://ducttapeeinstein.com/emit-mesh-prefabs-with-particle-system/
Mh, well thanks, I’ll check that out ![]()
And thanks to BoredMormon for the help ![]()
Why the loop in your code?
I know this is a seriously old thread, but what an amazing solution for this thing, I truly gotta say I liked this approach.![]()