Search Unity

Dynamic Maze Issue

Discussion in 'Game Design' started by scossgrove, Jan 12, 2016.

  1. scossgrove

    scossgrove

    Joined:
    Jan 6, 2016
    Posts:
    35
    Hi,
    I have a prefab wall which has a mesh render and mesh filter and box collider. This, i believe, enable basic collision detection and stops the player/enemies walking through the wall. I then dynamically generate about 1600+ of these prefabs.

    My problem is i noticed that game slows right down as I increase the number of these walls. I have decided to try and combine all the prefab meshes together after they are generated. This seems fine to me but I would like to know how to retain the individual collision meshes so i can tell which wall was collided with?

    Can anyone help?
     
  2. tedthebug

    tedthebug

    Joined:
    May 6, 2015
    Posts:
    2,570
    Does it need a mesh collider? Could you get away with a box collider or something else that's simple?
     
  3. JoeStrout

    JoeStrout

    Joined:
    Jan 14, 2011
    Posts:
    9,859
    Yeah, 1600+ objects is getting pretty heavy-handed!

    This is a somewhat tricky situation to use with the physics engine, because either you need to have 1600+ box colliders, which may not perform well; or you need a giant (non-convex) mesh collider, which won't collide with other mesh colliders (unless maybe they're convex).

    But do you really need the physics engine here? Pac-Man didn't have a physics engine and it kept everybody confined to the maze quite nicely. It's just a matter of doing the math — and it's fairly simple math, since everything is rectilinear.

    But if you must... Well, how are you combining the meshes? If you're using some third-party utility, you should check whether it has the option to leave the colliders alone. If not, you can certainly take matters into your own hands by, before you combine the meshes, iterate over them and move them onto another object. That will leave you with the 1600+ original box colliders.

    But again, let me encourage you to consider tackling this with math instead of with the physics engine. It's just going to perform about a zillion times better than any physics-based approach.

    (An in-between option would be to generate a NavMesh, and use that to limit motion instead of collisions... but I would still just do it myself in this case.)
     
    GarBenjamin likes this.
  4. daneislazy

    daneislazy

    Joined:
    Feb 3, 2015
    Posts:
    1
    It really depends on what is the cause of the slowdown. If it is just rendering, and you don't need to see all the walls constantly, you can disable the game objects, or more specifically the mesh renderers, as toggling the physics colliders has heavier cost. Using a trigger volume with onEnter/exit scripts to toggle the renderers, as the player/camera moves around, might be fine.
    If your wall mesh is complex/detailed, you could also reduce the complexity and use a Normal map.

    If the physics engine is also having a problem that is a bit trickier. Probably use object pooling to remove Instantiate's, then do a similar thing as above, but with moving the walls around to disassemble and reassemble the maze. That one will be harder to manage though.
     
  5. dogmachris

    dogmachris

    Joined:
    Sep 15, 2014
    Posts:
    1,375
    I think the problem is the colliders, especially if they're mesh colliders. They're pretty expensive if numerous in number.

    You could collect the positions of your walls to a vector3 list, disable all colliders and only check for the distance between the player & the walls by iterating through the list. Then activate only the colliders on the walls that have a distance < "something" to your player.

    Iterating through a vector3 list should be cheap, even if you have tens of thousands of entries.
     
    JoeStrout likes this.
  6. Master-Frog

    Master-Frog

    Joined:
    Jun 22, 2015
    Posts:
    2,302
    1600 3D objects is just a lot of objects. I would begin to seriously worry about performance with only 100-200 2D objects, for PC peformance.

    You're either going to have to shrink the level or you're going to need to use a more technical solution. I am not sure what your ability level is, so I am not going to elaborate unless you respond.
     
  7. TEBZ_22

    TEBZ_22

    Joined:
    Jan 28, 2014
    Posts:
    37
    I've made a 4D randomly generated maze with ruffly 12k box collides (and the same amount of boxes as walls, floor & ceiling) , and that was nicely playable both in my PC and in the old unity web player in a PC.

    Have you tried using the Profiler?

    /Thomas