Search Unity

More Efficient 2D Procedural Terrain

Discussion in 'Scripting' started by alexander_q, Sep 12, 2014.

  1. alexander_q

    alexander_q

    Joined:
    Feb 7, 2014
    Posts:
    32
    I'm procedurally generating some terrain using this script I made for the purpose, found here. The terrain is made up of individual objects with 64x64pixel sprites. I'm now faced with giving each of these pieces a uniquely configured edge collider to prevent the player walking off the edge of the terrain. See the image below.



    The terrain will contain hundreds of such pieces, each with a collider. My question is: is there a more efficient way I could be doing this?
     
  2. secondbreakfast

    secondbreakfast

    Joined:
    Jan 5, 2013
    Posts:
    98
    look at your draw call stats. I think as long as the stories are unlit they can be batches so it really only renders a sprite once and copies the data to each location. Think it's fine as long as your draw call batching is working properly.

    As for the collider on each one. It would be better to reduce the number by putting one that covers a larger area possibly on a parent game object that represents the land mass as a whole instead of the actual sprite
     
  3. StarManta

    StarManta

    Joined:
    Oct 23, 2006
    Posts:
    8,775
    If you know where the player is, you could disable all the edge colliders except the ones on his current tile (and possibly its immediate surroundings - 9 edge colliders is no problem at all).
     
  4. alexander_q

    alexander_q

    Joined:
    Feb 7, 2014
    Posts:
    32
    I've got draw calls: 4 and saved by batching: 19. Since the terrain is generated at runtime, how would I go about having a single collider on a parent object in the right shape?

    If I pursue the method of disabling unnecessary tiles, how can I gauge whether it is more efficient?
     
  5. StarManta

    StarManta

    Joined:
    Oct 23, 2006
    Posts:
    8,775
    Well, you make both of them, toggle each one with a keypress, and see which one runs better.

    I think the disabling of tiles will be faster, though, and dramatically simpler to code too. And it'll definitely scale better to larger and larger worlds.
     
  6. alexander_q

    alexander_q

    Joined:
    Feb 7, 2014
    Posts:
    32
    Thanks StarManta. What I mean is do I look at FPS and draw calls? Or is there some other measure that I might neglect if I only look at those?