Thought on AI / FSM design patterns for many npc's in Unity

This is a very open ended topic. I’ve gotten through C# to where I’m comfortable reading the advanced options it offers and used it to build my own quaint scripted framework for a game.

Unity has an inherent disadvantage for many parallel AI’s because it is single threaded. Still due to my lack of experience and the great documentation and community it’s my best choice. So it’s sort of a squeezing the fruit type question. “Simple” FSM AI’s have always fascinated me. I have a couple formal resources on the topic here I’m about to dive into now that I can handle C#, but I’d like to start an open discussion.

I’d like to support hundreds of concurrent, but simple AI’s capable of reacting to each other while they’re on the move. Initial thoughts I have before studying, your general thoughts on any of these ideas are welcome:

(thinking top down RTS-like perspective though I don’t think it’s relevant)

  • They will “think” in two dimensions. I position them visually in a third.

  • I know how to raycast them to a ground position on a static world mesh.

  • If I could do this task in a vertex shader on the GPU somehow that would be huge. Unfortunately none of the great Cg / ShaderLab references I have do anything like this, and it’s beyond me.

  • Batch process many AI’s. Each entity is a data object referencing some Unity prefab, time-sharing a single brain.

  • Perhaps they have some minimal contingency processing they all get every n frames, handled in subsets each frame. Here a proactive check such as an overlap may prioritize or flag them for special treatment.

  • Inside-out thinking. Static structures collect all nearby entities and inform them of behavior.

  • Actual multi-threading. Reduce some aspect of the AI’s processing to purely operations I can do myself in C# without the help of Unity API functions and write my first mutual exclusion locks. This will require significant formal study.

In no sense am I trying to build Factorio, but this is my general topic of interest. I seek any known resources free or not.

For drawing a lot of NPCs, look into instancing.

If you can represent both state and everything the NPCs need to react to by pure data (ie. no Unity API usage), you push their AI either to a different thread (easier), or to a compute shader (harder, much faster if you’re CPU bound, which can happen quickly if you’ve got a lot of AI going).

To get a sense for how multithreading interacts with the Update-loop, you should probably try some basic threading work. Thread Ninja hooks into the coroutine system to allow yielding into a different thread.

In my experience, time-sharing is very easy, and gives a huge boost in performance. Having your AI update every 10 frames gives pretty much exactly the same experience as every frame, at a tenth of the cost.

Oh man those are good links, thanks. I knew about draw call batching but not instancing, I’m probably not going to touch compute shaders because I’m only able to Frankenstein HLSL.

Thread Ninja looks like a great way to get started there. Been brushing up on my basic trig, I think it’s gonna pay off in no time.