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;
}
}
}
Answer by Chiroculon · Apr 11, 2017 at 06:41 AM
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.
$$anonymous$$y future plans for the NPC AI is to make each npc intractable, so I will be using Random.Range function to randomly select between preset personalities, then each one of those will have different quests, conversations or sayings selected in the same way I wanted to have a $$anonymous$$ain script that is essentially just the update function that will check each scenario and then act accordingly and have everything else saved as separate classes attached to the npc so that the main can call on it when needed also to make it easier to edit lines of code. my main concern is whether or not having too many if statements and bools in one function will make it too heavy so to speak
I scrapped this comment about seven times. Each time I write this comment I get a different idea on what to say. (facepalm) So I'll keep it short:
I think you should consider using finite state machines. (FS$$anonymous$$s)
$$anonymous$$aybe also think about a behavior priority queue.
Answer by Cuttlas-U · Apr 12, 2017 at 05:22 PM
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 :
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 :)
Good Luck
Answer by sundhar_unity · Oct 27, 2020 at 04:45 AM
This video might Help for NPC Movement : https://youtu.be/BIyYldTyyR8
Your answer
Follow this Question
Related Questions
Several NPC with waypoints 1 Answer
Enemy AI script for 2D platformer 1 Answer
My NPC keeps running through hills and floating 0 Answers
Fixing enemy behavior state machine in Unity 0 Answers
How to make an npc/ai walk next to the player? (C#) 1 Answer