Advice on building an NPC AI

So I am starting work on my first npc ai I would like some advice on what is the best approach to do so.I already have the navigation sorted. I was planing on using trigger boxes (to detect interactions) if statements and boolean’s over different ‘case’ classes all controlled by a main class to essentially build a logic tree to handle situations. is this the best approach? or is there a more efficient way of doing this?

an example of some code:

publi class npcAiMain : MonoBehaviour {

Public Transform player; //to track players position
public bool playerInRange; //if true npc can hear or see player
public bool playerInsight; //if true npc and see player (determined with RAY)
public bool crimeCommited; //if true npc reports crime if playerInSight true

void Update ()
{
  player = GameObject.FindWithTag("Player").transform;
//using FindWithTag as player tag is changed depending on vehicle or not 
 if (playerInRange == true) //check if player is in range
  {
    checkSight(); //check if npc can see player
  }

 if (playerInSight == true && crimeCommitted == true)
  {
    reportCrime();
  }
}

void checkSight();
{
   Vecto3 pos = transform.position; //determine npc position
   Vector3 target = player.position; // determine player position

  RayCastHit hitInfo;

if (Physics.Raycast(pos, target, out hitInfo, 20f)
{
  if (hitInfo.transform.tag == "Player")
     {
           playerInSight = true;
      }
else 
    { 
            playerInSight = false;
     }
   }
}
void OnTriggerEnter(Collider other)
	{
		if (other.tag == "Player")
        {
            playerInRange == true;
        }
	}
    void OnTriggerExit (Collider other)
    {
        if (other.transform.tag == "Player")
        {
           playerInRange == false;
        }
    }

}

For what this does so far, this code seems pretty efficient.

This way might be exactly what you need, but that actually depends on your future plans for the NPC AI.

hi;
for detecting u better use raycast;
trigger system is not good as some conditions like there is a wall between u and your enemy but trigger goes through it;

i cant tell u all but u can check this tutorial below i promise its very good and optimized ;
i used it once before in an android game ;
Field of view visualisation

and fore conditions try to use Enum function if u already know how to work with that else search a little there are some tutorials about using enum for creating AI;

oh wait;
there is a new good way to creata ai by using sciptable object introduced by unity u can see in here :

AI

sorry cant helo u much with the coding i can just give u an idea on how to do it because its a long way :slight_smile:

Good Luck

This video might Help for NPC Movement : Unity AI Random Movement ( Based on Area and Based on enemy) - YouTube