Optimization

Some tips on how to improve performance.

There are various ways to increase the performance of the system.

  • Profile! Before trying to optimize anything, use a profiler to figure out what part of the code that is slowing down the game. Note that if multithreading is used then pathfinding will not show up in the Unity profiler as it does not run in the main thread.

  • By far the easiest way is to enable multithreading (A* Inspector -> Settings). This will make the pathfinding run on a separate thread instead of on the Unity thread which will likely improve performance quite a bit since almost all computers and even phones have multiple cores these days.

  • Make sure "Show Graphs" (bottom of the A* inspector) is disabled. For large graphs the overhead of drawing it every frame can severely slow down the game (this only applies to the editor, not a standalone game).

  • Logging path results has a pretty large performance impact. Often as much as reducing the pathfinding throughput by 50%. You should disable path logging if you do not need it. Set A* Inspector -> Settings -> Path Log Mode to None.

  • Another thing to think about is what graph type you are going to use. Grid graphs is the easiest type to get started with but it cannot handle very large worlds (kilometers or larger in size). The recast graph can do that and is also a lot faster than the grid graph at smaller sizes since it usually contains a lot fewer nodes. The drawback is that it is slower to scan and is not as flexible when it comes to updating it during runtime. But if you have a static level you almost definitely should go with a recast graph.

  • The different included movement scripts have different performance characteristics. The AILerp script is the fastest, but the movement doesn't look very realistic which may or may not be acceptable for your game.

  • Try to avoid using the CharacterControlller component if you have many units. It is often much faster to remove it and instead use a simple raycast to detect where the ground is (this is possible out of the box with the AIPath and RichAI movement scripts).

  • Reduce the frequency at which new paths are calculated. This is controlled by the 'repathRate' field on the movement scripts.

    • Perhaps you can even disable automatic path recalculation and only recalculate the path once when the character starts moving?

    • Or you could customize the automatic path recalculation to recalculate the path less often if the character is very far from its target and more often when it is close?

  • Use lower quality settings on modifiers. In particular you might be able to reduce the number of segments that the SimpleSmoothModifier divides the path into if you are using it. The AIPath movement script already does a pretty good job at following the path smoothly even if the path itself is not particularly smooth.

  • If you use the AIPath or RichAI scripts:

    • If you have a lot of AIs, then open the AIBase.cs script and remove either the Update or FixedUpdate method depending on if you are using rigidbodies or not for moving the character (remove FixedUpdate if you are *not* using rigidbodies). Otherwise Unity still has to call the other method which can add up to a significant overhead if you are using a lot of AIs.

      See

      https://blogs.unity3d.com/2015/12/23/1k-update-calls/ which introduces a more advanced method. This has not been implemented in this package to avoid increasing the code complexity. If it makes sense for your game you may want to implement it.

      See the child pages for more ways to increase the perfomance.

  • Heuristic Optimization

  • Pooling

  • Compiler Directives