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. Dismiss Notice

Good way to randomly spawn objects on a 2D plane?

Discussion in 'Editor & General Support' started by osum4est, Mar 5, 2015.

  1. osum4est

    osum4est

    Joined:
    Jan 26, 2015
    Posts:
    13
    Hello, I'm creating a 2D mobile game where you fly a ship around in space. It is all in a top down view. I want to spawn objects randomly as you fly along in space. I do not, however, want the objects to spawn within the camera's view. I would also like to delete objects as you fly far enough away from them to prevent tons of objects being handled by the game. If the user flys back to where they were previously, where the objects got deleted, new objects should spawn.

    I don't need exact code, just a good concept of how to write it. I'm kind of stumped trying to think of an efficient way to do it.

    Any ideas? Thanks!
     
  2. osum4est

    osum4est

    Joined:
    Jan 26, 2015
    Posts:
    13
    Anything?
     
  3. vovkasolovev

    vovkasolovev

    Joined:
    Jul 10, 2013
    Posts:
    5
    I had a similar task in my game. Came to this decision: I create the «Spawn circle» and the «Remove circle» trigers around the player, as his childs. The «Spawn circle» is always greater than the visible region on screen. The «Remove circle» always greater than the «Spawn circle». On the edge of the «Spawn circle» I spawn a random game objects (marked with Tag «PickUps», for example) from the Array list of avalable objects. When a game object with «PickUps» tag leave bounds of the «Remove circle», add him back to the Array for reuse. This allow me don't think about every object, and don't destroy them everytime for better perfomance.
     
  4. osum4est

    osum4est

    Joined:
    Jan 26, 2015
    Posts:
    13
    I like your idea! So the objects spawn somewhere random in the spawn circle? How do you make sure they don't spawn within the camera?
     
  5. vovkasolovev

    vovkasolovev

    Joined:
    Jul 10, 2013
    Posts:
    5
    I take long side of the screen as variable ScreenSize, then set diameter for Spawn as ScreenSize *2 and ScreenSize * 4 for Remove. Objects spawn not inside the Spawn, they spawn on edge of the Spawn circle, on constant distance from player in any direction. Also I update ScreenSize variable on resize of screen, to make game ready for any screen resolution. (And make math for amount of objects in ArrayList to keep constant uniform distribution of objects on screen)
     
  6. osum4est

    osum4est

    Joined:
    Jan 26, 2015
    Posts:
    13
    Cool, looks like a good idea. I have come up with my own, now though. I'm using a grid spawning spawning method. Fly into a new box, and the surrounding ones spawn things. When objects get to far away, they get destroyed!

    If you want me to go into more detail I can!