Hello there! I’d really want to learn the basics of creating AI in unity. I do know how to create the follow player AI. But I don’t know how to make AI that works on his own. I want to have no controll over the AI in any way possible! My goal is to make something like. Sheeps and wolves. The sheep walk around and try to eat food. While the wolves try to hunt the sheep for food. (and the sheep running away of course)
Thank you!
-Wiebe
@JoeStrout
So I recommend a “Utility AI” approach for this, sometimes also known as “drive reduction AI.”
Basically you give the agents (sheep, wolves, whatever) a set of drives: eat food, stay safe, sleep, etc. Each of these will increase at some steady rate, or increase much faster in response to what’s around them (e.g. when a sheep sees a wolf, it’s “stay safe” drive should increase much faster than when it doesn’t).
Then you give them actions which can reduce those drives. Eating reduces the Eat Food (aka hunger) drive; fleeing a wolf or hiding reduces the Stay Safe drive, sleeping reduces the Sleep drive, etc.
At any point in time, you basically look at the available actions, and pick the one that reduces the biggest drive the most.
There are various wrinkles you can apply, such as giving more weight to continuing the present action rather than switching to a different action. But that’s the basic idea. With this architecture, it’s pretty easy to add new drives and actions, and you don’t need a big logic tree or state machine; they’ll just naturally get worked in with all the others.
The downside to this approach is that there are a fairly high number of free parameters: the rate at which drives increase, the amount by which actions decrease them, etc. You have to get all those reasonably balanced to get good behavior. But fortunately, it’s not too sensitive; there are a wide range of values that will produce pretty good results.
As for reference, I know some of the books in my shelf cover this (I think Artificial Intelligence for Games does for example). But if you google “utility AI” and “drive reduction AI” I’m sure you’ll find something there.
Also, here’s an introductory article from Game AI Pro:
http://www.gameaipro.com/GameAIPro/GameAIPro_Chapter09_An_Introduction_to_Utility_Theory.pdf
Depending on just how versatile your AI needs to be, you could try Goal Oriented Action Planning, or GOAP. It’s similar to JoeStrout’s Utility AI: you give an AI a set of parameters, or goals, and provide it with a list of actions it’s capable of taking. Each action has a cost and a set of preconditions, which can vary. In my implementation, there’s a “world state” that the AI sets, grabbing it’s health and whether or not it has a target, as well as a few other things, and “procedural preconditions,” which tell whether or not the AI can reach a certain position on a NavMesh, or whether or not there’s too many AI’s attacking the player at any one time. The actions all satisfy goal or multiple goals, and the AI picks a sequence of actions to satisfy the goals it has.
There’s a good article and an example here. I use this base example for my RPG-ish game. It’s extremely versatile, but less efficient speed-wise than, say, a finite state machine. If you need something relatively simple or specific (like AI for a shooter, or a maze solver), you’d be better off using a more fine-tuned approach.
EDIT: Came across a more generic implementation of finite state machines that you can probably use for anything. Posting the link.
Thank you for replieng! All of you, I started searching for “finite state machine” first. Because that looks like the first step I should do for creating an AI. I found a great series on building a finite state machine on youtube. He uses the book. Creating videogame AI by example. It is a great tutorial. I will watch the tutorial first. Before I start creating the AI.
This is the youtube tutorial I found:
Note: this is actually the second episode of the tutorial