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

Best practices for large mobs

Discussion in 'Scripting' started by jesus_the_jesus_man, Nov 29, 2014.

  1. jesus_the_jesus_man

    jesus_the_jesus_man

    Joined:
    Nov 19, 2014
    Posts:
    6
    Title said it, I'm working on a school project to create a RTS. I've been struggling rather hard with mobs given that when to many are spawned (200-300) my framerate goes to hell. This is being designed to be on a mobile device so that might be affecting it a bit. So any good advice?
     
  2. GarBenjamin

    GarBenjamin

    Joined:
    Dec 26, 2013
    Posts:
    7,441
    If 200 to 300 are killing your frame-rate... a great solution would be to have less than 200.
     
    RJ-MacReady likes this.
  3. RJ-MacReady

    RJ-MacReady

    Joined:
    Jun 14, 2013
    Posts:
    1,718
    Redesign your game to accommodate the hardware limitations.
     
    GarBenjamin likes this.
  4. jesus_the_jesus_man

    jesus_the_jesus_man

    Joined:
    Nov 19, 2014
    Posts:
    6
    any tips along the lines of how to better optimize or better practices to try and get closer to my goals? because telling me to not have more then 200 is sorta an obvious one...
     
  5. RJ-MacReady

    RJ-MacReady

    Joined:
    Jun 14, 2013
    Posts:
    1,718
    Obvious? Must be wrong, then.
     
  6. jesus_the_jesus_man

    jesus_the_jesus_man

    Joined:
    Nov 19, 2014
    Posts:
    6
    just saying, if starcraft could have 600 units with less then a GHz of processing I believe a phone would be able to at least do 300...
     
  7. RJ-MacReady

    RJ-MacReady

    Joined:
    Jun 14, 2013
    Posts:
    1,718
    That's not scientific at all. A gameobject in Unity is more complex than whatever they're doing in starcraft, I'm sure. It comes down to overhead, how many processes your units gobble up every update, etc. Can you display 300 static game objects, just sitting with no scripts attached, without lag? Test that out and let me know.

    Edit: Not to mention most likely you're not running out of processing power, you're moving too much memory around.
     
    Last edited: Nov 29, 2014
  8. jesus_the_jesus_man

    jesus_the_jesus_man

    Joined:
    Nov 19, 2014
    Posts:
    6
    oh, if there just sitting there I've hit about 1k on my rather old phone, the main issue comes when they start moving and colliding about.

    As far as memory vs processing it actually does boil down to processing, got a feeling navigation/collision might be the biggest guzzler.
     
  9. RJ-MacReady

    RJ-MacReady

    Joined:
    Jun 14, 2013
    Posts:
    1,718
    Well, you know best, after all.

    Well, everything Unity does is built for convenience. If you want really low "processing requirements" (since we can't discuss memory operations) you'll need to write everything yourself in C/C++.

    ...which is faster, you know, because C/C++ commands run faster on a computer processor, right?
     
  10. jesus_the_jesus_man

    jesus_the_jesus_man

    Joined:
    Nov 19, 2014
    Posts:
    6
    oh boy, I bet you must be fun at parties...
     
    0tacun likes this.
  11. RJ-MacReady

    RJ-MacReady

    Joined:
    Jun 14, 2013
    Posts:
    1,718
    Sorry I'm too busy writing programs that work to party
     
  12. 0tacun

    0tacun

    Joined:
    Jun 23, 2013
    Posts:
    245
    It would be good if you could provide a bit more info of your Ai.
    What are they doing and in what kind of environment? Are you killing and spawing a lot npcs? Try object pooling instead of instantiating and destroying every single one. How are you moving them? Do you manage your army in squads like in Company of Heros or even Total War? How often are you accessing the Ai script? Every frame? Would every second be sufficient?

    I think someone posted on this forum a prototype with 3000 npc battling against each other. Maybe you find some info.
     
    JasonBricco likes this.
  13. RJ-MacReady

    RJ-MacReady

    Joined:
    Jun 14, 2013
    Posts:
    1,718
    Oh boy... This would really be boring at a party.
     
    mrmisterboi likes this.
  14. Whippets

    Whippets

    Joined:
    Feb 28, 2013
    Posts:
    1,775
    Certainly every frame will kill everything with 200-300 NPCs all doing stuff. I run my AI at 1 second and my npc/mob movement at a FixedUpdate of 0.2s - but that's on a dedicated server with enough poke to handle it all.
     
  15. TonyLi

    TonyLi

    Joined:
    Apr 10, 2012
    Posts:
    12,523
    I think there's room in this forum for game architecture design, in addition to the gameplay design that most of the threads have been about.

    I'll second what the others are writing: identify the culprit. If you have a profiler, use it. Otherwise isolate and test the likely suspects:
    • Rendering: Can you draw 300 static units (are they 3D models or sprites?) with nothing else happening? No animation, no physics components, no navigation, no user input, no decision-making AI, etc. If rendering is the bottleneck, reduce the polygons, use faster shaders, decrease the view area, use occlusion culling, improve batching, decrease draw calls, etc. If you can, identify whether it's a CPU or GPU issue, and check everything on the Optimizing Graphics Performance page.
    • Animation: Can you animate those 300 units? (What animation system - Legacy or Mecanim?) Maybe you have a crazy blend tree that's taking too long to resolve, or your high-level animation control script(s) are the culprits.
    • Physics: If you add in physics, does it run fast enough? If not, simplify your colliders (e.g., don't use mesh colliders), adjust timesteps, and generally check everything in Optimizing Physics Performance.
    • Navigation: Presumably your units are pathfinding. If you're using NavMesh, make sure you're not recomputing the path every frame. You might need to switch to a simpler pathfinding solution. But first you need to identify if this is the problem.
    • AI: Does the decision-making AI impact performance? If so, on the individual unit level, or the AI player strategic level? As others have suggested, can you make it more efficient or reduce the frequency of AI updates? (Again, only do this if you've identified AI as the culprit.)
    • Other: Are you using OnGUI? If so, are you doing anything other than straight drawing? Every OnGUI invocation has significant overhead, and it's called multiple times per frame. You shouldn't do any computations in it.
    You first have to know what parts are causing the slowdown, and how much each part is contributing. Once you've identified that, post back here if you're looking for tips or ideas. It's likely that others have had to overcome the same issues.
     
    0tacun likes this.
  16. CaoMengde777

    CaoMengde777

    Joined:
    Nov 5, 2013
    Posts:
    813
    you could maybe group like.. 5-10 men to act as one unit.. they all move together and stuff..
    it could help a little.. just a thought