An (In Depth) Introduction to Better NPCs
Posted 2 days ago
Targeting and Awareness is generally where the heavy lifting of most game NPCs happen so improving those behaviors should be a priority. In "Aim Improvements" I used Second Order Dynamics (a system that uses a math formula known as Second-Order ODE) to simulate these behaviors. Long story short the NPC aims more akin to natural player movements.

Here is a spectator view of uninterpolated NPC aim compared to using Second Order Dynamics

As far as awareness goes there's generally a few methods used for NPCs, with scene based engines like Unity Engine NPCs will either use triggers to find all NPCs in an area then filter them out with a line-of-sight raycast and/or find objects with a component or tag. This method works and can be optimized further in the larger scale by even incorporating chunks so only nearby entities are searched for but there's just a lot to be desired.

Here I'm experimenting with a method I've only seen in a random indie game devlog (expanded much further to fit my preferences of course). This system instead uses raycasts which on their own are very efficient but still have a cost so it's good to still keep optimization in mind.

As many lines being drawn I run the checks on FixedUpdate instead of the standard Update loop for frame stability and I'm only checking for collisions to further improve optimization, even with a very heavily dense set of rays I can still run ~20 NPCs (all awake so still running all of their logic) before dipping below 60 fps.

The real power comes from what you can detect from patterns in the rays. Unfortunately I didn't get around to it but the original plan was for this to both help object detection and dynamic navigation. If I were to make it so I use standard NPC object detection I would be able to significantly reduce the amount of rays used.

I'm hoping someone reads this and tries tackling doing completely dynamic locomotion using raycasts. I love navmesh and since the navmesh in S&Box is really quick to update there's no real reason to take on a project like this other than for the pure fun aspect of it. There are still imperfections when it comes to hammer and navmesh so maybe doing a hybrid system could be promising