Search Unity

Reusable code for animated AI NPCs?

Discussion in 'Animation' started by atcarter714, Aug 6, 2021.

  1. atcarter714

    atcarter714

    Joined:
    Jul 25, 2021
    Posts:
    10
    I'm wondering what would be the best way to design some reusable code for AI-controlled NPCs with animations. Like how should I design the GameObject hierarchies and what to put into the scripts to make a reusable architecture that can be specialized for different types of NPCs. As it stands, I got all this stuff to work in a single NPC GameObject, even implemented realistic IK foot placement, but it's so messy and specific it's not portable at all ... only the IK foot placement is reusable.

    How should I go about setting the value range in my animation blend trees, and what script should be setting them each frame? Where should I extract that "speed" value from to determine what value to set? The way I did it so far is by taking the navmeshagent velocity, converting to magnitude and determining what % of its max speed it's traveling to set a blending value between 0 and 1 (0 is idle, 0.10 begins walking and 1.0 is full sprint). But I'd like to have some kind of portable "Character" class that's going to figure out how fast a character is moving through the world (regardless of what's moving it -- whether it's the navmeshagent or some custom logic or even the editor move tool) and automatically make the animations match the movement speeds and directions. Basically, I want his feet to be doing the right thing to match how he's moving at all times. And of course I can interrupt that for things like making him stumble from being hit or something.
     
  2. atcarter714

    atcarter714

    Joined:
    Jul 25, 2021
    Posts:
    10
    Also note, I'm considering making the AI base class abstract so that it has to be inherited by more specialized classes like NPCEnemyAI or NPCFriendlyAI, and those can be derived from to create more and more specific versions and differentiate between something like a human opponent or an orc which each have their own special abilities and attack patterns. I like to have a good abstract base that handles all the things common to both with abstract/virtual methods that can be overridden to implement specialized and specific behavior. And I'd even like to use common patterns in animation controllers where the specific motions can just be swapped out to match the type of character, such as a female human having the same controller logic but specific motions different from a male, and an orc or monster having its own unique motions as well but re-using animation controller logic as much as possible.
     
unityunity