- Home /
How to set priority over certain actions?
Hello. So I have things in my game for my NPC AI that I've been thinking of as actions, but it is basically a large chunk of code in a method known as something like "Find Food" or "Run From Enemy". Obviously running from an enemy is far more important than finding food, and the NPC can only focus on one of these at a time, so how do I give an action such as that a priority over other less helpful actions? Of course, I could do something such as the code below, but that looks like a horrible way to do it and I'd have to modify all the code everytime I add a new action for the NPC to take.
//Bad looking way for letting the NPC choose which action to take.
if (Enemy != null){
EvadeEnemy ();
} else{
FindFood ();
}
Answer by NGC6543 · Jul 03, 2018 at 04:51 AM
Hmm, How about creating an another method that manages the current 'state' of the AI? In there your AI can evaluate the current situation and can decide wether runaway from an Enemy or FindFood. Also a Mecanim(AnimatorController) can be used to implement the AI state.
The way you implemented above can cause 'jitter' on the boundary condition, and not extendable, either.
Tip Try implementing the 'another method' using an IEnumerator. You can manage the CPU resources needed to calculate the state.
IEnumerator EvaluateAIState() { while (true) { // Calculate AI state here // You can deter$$anonymous$$e how often the state should be updated. yield return new WaitForSeconds(1f); } }
Hope this helps!
Your answer
![](https://koobas.hobune.stream/wayback/20220612165931im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
A set of actions 1 Answer
How to use A* pathfinding for a large map? 2 Answers
No gravity on NPC. Floating above the ground. 2 Answers
Make NPC follow player when seen? 2 Answers