Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Procedural infinite asteroid field

Discussion in 'Scripting' started by Fra123X, Sep 4, 2015.

  1. Fra123X

    Fra123X

    Joined:
    Mar 10, 2013
    Posts:
    40
    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?
     
  2. Kiwasi

    Kiwasi

    Joined:
    Dec 5, 2013
    Posts:
    16,860
    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.
     
  3. Fra123X

    Fra123X

    Joined:
    Mar 10, 2013
    Posts:
    40
    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:

    Code (C#):
    1.  
    2. void Start () {
    3.      for (int i = 0; i < numberOfAsteroids; i++)
    4.         Instantiate (asteroids[Random.Range (0, asteroids.Length)], transform.position + Random.insideUnitSphere * radius, Random.rotation);
    5. }
    6.  
    I guess I'll have to use the Update function for this one, but how could I implement it?
     
  4. Kiwasi

    Kiwasi

    Joined:
    Dec 5, 2013
    Posts:
    16,860
    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.
     
    Fra123X likes this.
  5. Fra123X

    Fra123X

    Joined:
    Mar 10, 2013
    Posts:
    40
    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:

    Code (C#):
    1.  
    2. void OnTriggerExit () {
    3.     float radius = //Get the radius from the other script;
    4.     Vector3 newSphereCenter = GameObject.FindGameObjectWithTag ("Player").transform.position + GameObject.FindGameObjectWithTag ("Player").transform.forward * radius;
    5.     Vector3 nextPos;
    6.     do {
    7.         nextPos = newSphereCenter + Random.insideUnitSphere * radius;
    8.     } while (Vector3.Distance (nextPos, GameObject.FindGameObjectWithTag ("Player").transform.position) < radius);
    9.     transform.position = nextPos;
    10. }
    11.  
    And it seems to be working, do you think some parts can be improved or is it good to go?
     
  6. BPPHarv

    BPPHarv

    Joined:
    Jun 9, 2012
    Posts:
    318
    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/
     
    Fra123X likes this.
  7. Fra123X

    Fra123X

    Joined:
    Mar 10, 2013
    Posts:
    40
    Mh, well thanks, I'll check that out :)
    And thanks to BoredMormon for the help :D
     
  8. Kiwasi

    Kiwasi

    Joined:
    Dec 5, 2013
    Posts:
    16,860
    Why the loop in your code?
     
  9. no00ob

    no00ob

    Joined:
    Dec 13, 2017
    Posts:
    65
    I know this is a seriously old thread, but what an amazing solution for this thing, I truly gotta say I liked this approach.:D