Search Unity

C# HELP with Spawning prefabs for runner.

Discussion in 'Scripting' started by amichael, May 5, 2015.

  1. amichael

    amichael

    Joined:
    Mar 4, 2015
    Posts:
    48
    Hey guys, im trying to find a way to spawn my prebuilt obstacles (background, objects to avoid) one right after another in my runner. What would be a way to spawn and destroy the prefabs smoothly while the player is running through the obstacles? I am new to all this, just trying new methods to get things done. I really appreciate the help in advance.
     
  2. Korno

    Korno

    Joined:
    Oct 26, 2014
    Posts:
    518
    Some quick hints -

    assuming you runner is always running in a straight line, you could just spawn stuff x units ahead of him every x amount of seconds. Look at coroutines for doing that. They will allow you to pause your spawning method for an amount of time. As for destroying the objects, used renderer.isVisible or simple vector distance checks to see if the object is still visible or behind the player. renderer.isVisible returns false when the camera cant see the object (i.e. wont render it )

    Thirdly, as I guess this is a mobile game - Object pooling is your friend. Dont destroy, or create game objects during gameplay as this frees and uses memory - causing the garbage collector to run - not a good thing on mobile.
     
  3. hamsterbytedev

    hamsterbytedev

    Joined:
    Dec 9, 2014
    Posts:
    353
    If you are using this on mobile I offer a professional object pooling asset. It's called pool master. You can find it in the unity asset store or from the link in my signature. It's only $9.99 and for what you get it is more than fairly priced. Good luck in all your future endeavours.
     
  4. SubZeroGaming

    SubZeroGaming

    Joined:
    Mar 4, 2013
    Posts:
    1,008
    You can check out my tutorial on Object pooling (check my signature).
     
    hamsterbytedev likes this.
  5. hamsterbytedev

    hamsterbytedev

    Joined:
    Dec 9, 2014
    Posts:
    353
    He's already purchased my asset, but it wouldn't hurt to understand how it works under the hood! Your tutorials are pretty good for someone who is just starting out and wants to learn some rudimentary stuff; pretty sure you have a few more advanced concepts too which is good. I could have used those a few years ago :)
     
    SubZeroGaming likes this.