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

Best way for spawning objects in an endless runner

Discussion in 'Scripting' started by Hertzole, Dec 11, 2015.

  1. Hertzole

    Hertzole

    Joined:
    Jul 27, 2013
    Posts:
    416
    Hi!

    I am working on something that resembles an endless runner (2D) and I am not sure which way of spawning my objects I want to do. The biggest concern is performance but still making it work best and still spawn objects with even spaces if the camera is moving faster.

    So currently I have a coroutine that runs for each "Spawn Group" as I called them. When the game starts a foreach loop runs for each spawn group and starts a coroutine for that object. This coroutine basically spawns objects at a random interval. But this has a few negative features. If the camera moves faster the objects will get separated further and further. This will in the end look weird. But on the plus side, it is easy to manage and it looks like it doesn't use a lot of performance.

    On the other hand, I've been thinking of another way to it using the Update function. Now I can already suspect this will be performance heavy but it seems like it would work pretty good. The plan here is to use a foreach loop for each spawn group and check the distance from the previous spawned object in this group with a new random value. If the distance is less than a value it would spawn a new object and set that as the previous object. This would work if the camera were going really slow or really fast. It would still spawn them with even spaces. But as I said, it would probably be very performance heavy. But (too many buts) this isn't something I can confirm. It is only what I suspect. If anyone could help approve/disapprove this it would be great!

    So to summarize the question. What would the best alternative to spawning objects be? A coroutine for each object or a foreach loop for each object in the Update function?
     
  2. Mich_9

    Mich_9

    Joined:
    Oct 22, 2014
    Posts:
    118
    You could put a trigger collider at the end of every Spawn Group, when the player enter in contact with that collider it will spawn the next group no matter the speed. ¿What do you think?.
     
  3. Hertzole

    Hertzole

    Joined:
    Jul 27, 2013
    Posts:
    416
    I am not so sure about this. I don't know why but it feels like it won't work very well. Some things need to be spawned quite close to each other and that would make so if the player enters a collider on the far left on the screen a new object could just pop into existence.
    I hope that made sense...
     
  4. Mich_9

    Mich_9

    Joined:
    Oct 22, 2014
    Posts:
    118
    Then put the Collier in the middle or even better, the beginning of a SpawnGroup, that will make the new group spawn while still is off screen.
     
  5. Diablo404

    Diablo404

    Joined:
    Mar 15, 2013
    Posts:
    136
    First, you should probably make a pool of GameObject ( Instantiate them at the start of the scene ) and the teleport the ones that have passed. This will prevent performance peek when instantiating object.

    Then, use the last position of the object to repositionate the following one right after.