{"id":92,"date":"2026-04-04T11:01:22","date_gmt":"2026-04-04T09:01:22","guid":{"rendered":"https:\/\/www.noocturnalgamesstudio.com\/blog\/2026\/04\/04\/dynamic-and-contextual-ai-in-unity-how-your-npc-will-prioritize-fleeing-chasing-or-investigating-sounds-without-a-giant-if-else\/"},"modified":"2026-04-04T11:01:22","modified_gmt":"2026-04-04T09:01:22","slug":"dynamic-and-contextual-ai-in-unity-how-your-npc-will-prioritize-fleeing-chasing-or-investigating-sounds-without-a-giant-if-else","status":"publish","type":"post","link":"https:\/\/www.noocturnalgamesstudio.com\/blog\/2026\/04\/04\/dynamic-and-contextual-ai-in-unity-how-your-npc-will-prioritize-fleeing-chasing-or-investigating-sounds-without-a-giant-if-else\/","title":{"rendered":"Dynamic and Contextual AI in Unity: How your NPC will prioritize Fleeing, Chasing, or Investigating Sounds (without a giant IF\/ELSE)"},"content":{"rendered":"<div style='font-family: system-ui, -apple-system, sans-serif; line-height: 1.8; color: #e2e8f0; font-size: 16px;'>\n<p style='margin-bottom: 15px;'> If you are an indie developer, you know this feeling: you open your AI script and find a 500-line monster full of nested conditions. &#8216;If the player is close, but my health is low, run; but if I hear a sound and see no one, investigate; but if an ally screams&#8230;&#8217;. <\/p>\n<p style='margin-bottom: 15px;'> The result is fragile code, hard to debug, and NPCs that behave erratically. The solution? Stop programming states and start composing behaviors. Today we will break down how the event-driven architecture of the <strong>Modular AI Behaviour Kit<\/strong> transforms this chaos into a professional and scalable system. <\/p>\n<h3 style='color: #39FF14; margin-top: 30px; font-weight: 700; letter-spacing: 0.5px;'> Ending the Spaghetti: Event-Driven Architecture <\/h3>\n<p style='margin-bottom: 15px;'> Most Unity tutorials teach you to use the Update() method to check distances every frame. It is inefficient. Our kit utilizes <strong>UnityEvents<\/strong>. &#8216;Sensory&#8217; components (like the PlayerDetector) emit events only when something actually changes. <\/p>\n<div style='background-color: rgba(255, 255, 255, 0.05); border-left: 3px solid #f687b3; padding: 15px; margin-bottom: 20px; border-radius: 0 8px 8px 0;'>\n<h4 style='color: #fff; margin-top: 0; font-weight: 600;'> Practical Example: <\/h4>\n<p style='margin-bottom: 0;'> The <strong>SimpleChase<\/strong> component does not search for the player. It subscribes to the <em>OnTargetDetected<\/em> event from the <strong>PlayerDetector<\/strong>. The moment the visual raycast confirms line of sight, the chase begins instantly. No unnecessary checks every frame. <\/p>\n<\/p><\/div>\n<h3 style='color: #39FF14; margin-top: 30px; font-weight: 700; letter-spacing: 0.5px;'> Smart Prioritization: The Coordinator Brain <\/h3>\n<p style='margin-bottom: 15px;'> How does an NPC decide what to do when two things happen at once? This is where the <strong>AIController<\/strong> comes in. This script acts as an orchestra conductor managing a Finite State Machine (FSM) based on Addon composition. <\/p>\n<p style='margin-bottom: 15px;'> The decision hierarchy is built into the controller logic: <\/p>\n<ul style='color: #cbd5e0; margin-bottom: 15px;'>\n<li><strong>Panic (Fleeing):<\/strong> If the NPC has a <em>FleeBehavior<\/em>, the controller gives it absolute priority over combat. Ideal for civilians or animals.<\/li>\n<li><strong>Aggression (Chasing):<\/strong> If there is no fear but a target is detected, it activates <em>SimpleChase<\/em> and the attack modules (Melee or Ranged).<\/li>\n<li><strong>Curiosity (Investigating):<\/strong> If the <em>SoundReceiver<\/em> detects a noise while patrolling, the controller transitions to the <em>Searching<\/em> state to go to the last known sound position.<\/li>\n<\/ul>\n<h3 style='color: #39FF14; margin-top: 30px; font-weight: 700; letter-spacing: 0.5px;'> Creating Personalities in Seconds <\/h3>\n<p style='margin-bottom: 15px;'> With the Modular AI Behaviour Kit, your enemy &#8216;personality&#8217; is not programmed, it is assembled. Look at these archetypes: <\/p>\n<p> <code style='display:block; background: #1a202c; color: #68d391; padding: 10px; border-radius: 5px; margin: 5px 0; font-family: monospace; border: 1px solid #2d3748;'> \/\/ Horde Zombie: <br \/> PlayerDetector + SimpleChase + MeleeAttack + Alerter <\/p>\n<p> \/\/ Tactical Archer: <br \/> PlayerDetector + SimpleChase (with Minimum Distance) + RangedAttack + SearchBehavior <\/code> <\/p>\n<p style='margin-bottom: 15px;'> By adding the <strong>Alerter<\/strong> component, a zombie that detects the player will &#8216;scream&#8217; (emit a social alert), activating all nearby NPCs with an <strong>AlertReceiver<\/strong>, even if they have not seen the player yet. This creates realistic group behaviors automatically. <\/p>\n<h3 style='color: #39FF14; margin-top: 30px; font-weight: 700; letter-spacing: 0.5px;'> Conclusion: Modularity is Power <\/h3>\n<p style='margin-bottom: 15px;'> Do not waste months reinventing the AI wheel. By separating perception (senses) from execution (movement and attack), you get a system that you can extend and reuse in any project, from survival horror to tactical RPGs. <\/p>\n<hr style='border: 0; border-top: 1px solid #4a5568; margin: 30px 0;'>\n<div style='background: linear-gradient(145deg, #1a202c, #2d3748); border: 1px solid #4a5568; padding: 25px; border-radius: 12px; margin-top: 40px; text-align: center; box-shadow: 0 4px 6px rgba(0,0,0,0.3);'>\n<p style='margin-bottom: 10px; font-weight: bold; color: #fff; font-size: 1.1em;'>Level up your game today<\/p>\n<p style='margin-bottom: 0; color: #cbd5e0;'>Get this asset on the <a href='#' style='color: #39FF14; text-decoration: underline; font-weight: bold;'>Unity Asset Store<\/a>.<\/p>\n<\/p><\/div>\n<\/p><\/div>\n","protected":false},"excerpt":{"rendered":"<p>If you are an indie developer, you know this feeling: you open your AI script and find a 500-line monster full of nested conditions. &#8216;If the player is close, but my health is low, run; but if I hear a sound and see no one, investigate; but if an ally screams&#8230;&#8217;. The result is fragile [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-92","post","type-post","status-publish","format-standard","hentry","category-sin-categoria"],"_links":{"self":[{"href":"https:\/\/www.noocturnalgamesstudio.com\/blog\/wp-json\/wp\/v2\/posts\/92","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.noocturnalgamesstudio.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.noocturnalgamesstudio.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.noocturnalgamesstudio.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.noocturnalgamesstudio.com\/blog\/wp-json\/wp\/v2\/comments?post=92"}],"version-history":[{"count":0,"href":"https:\/\/www.noocturnalgamesstudio.com\/blog\/wp-json\/wp\/v2\/posts\/92\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.noocturnalgamesstudio.com\/blog\/wp-json\/wp\/v2\/media?parent=92"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.noocturnalgamesstudio.com\/blog\/wp-json\/wp\/v2\/categories?post=92"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.noocturnalgamesstudio.com\/blog\/wp-json\/wp\/v2\/tags?post=92"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}