- Home /
AI in Untiy, best practices
THIS IS NOT A REQUEST FOR AN AI TUTORIAL. (sort of)
Likely, I'm just asking the wrong question. I have a pretty good grasp of how AI works, but I'm a still not completely sure how to write it so that its eloquent and modular. Basically I want to avoid having separate AI code for every enemy or character in the game but I'm having a hard time finding a good way to write it.
What I want is basically a singular script that is the base for all characters in, but then you can add to a list or node the specific behaviors that enemy or character has. Things like idle behavior, way-point patrol, pursuing and attacking player, etc. That way I can focus on making more verbose behaviors rather than having to create possibly hundreds of unique AI scripts for all the enemies in the game (since likely, many of them will have similar behavior patterns MINUS one or two things that make them unique).
I saw something like this demonstrated a while ago in a YouTube video but I can't for the life of me find that video again. So what I want to know: is this the best practice in Unity? If so, what are some suggestions (or tutorials specifically about this) that you can give me about writing behaviors this way? If not, what would be a better way to go about it?
Thanks, sorry for the lengthy post.
EDIT: It is also sort of my goal to avoid AngryAnt's behave. I'm both trying to grow as a programmer as well as uncomfortable with his licensing agreement, so I do not wish to use Behave for my solution.
Here is a good plugin, it may help you.
https://www.assetstore.unity3d.com/#!/content/87744?aid=1101l34jr
Answer by apollyonbob · Mar 16, 2012 at 12:51 AM
I see that you're trying to avoid AngryAnt's behave, but are you trying to avoid Behavior Trees? If not: http://aigamedev.com/insider/article/behavior-trees/ - It requires a signup, but it's free to watch that particular video. That's an excellent description of Behavior Trees and how they work.
My approach to it was to create an abstract Behavior Tree which could be overridden for specific purposes. So for "Shooting" for instance, I have a ShootTree: BehaviorTree. Then within that Tree, I create the nodes in the Start(). Each Node can be the various types of Behavior Tree nodes (Parallel, Serial, etc - a full discussion of Behavior Trees is out of the scope of this answer) which all implement the same Interface. Then when I create node, I inject the logic into the node as I'm creating it, including PreConditions, PostConditions, and the actual node logic. Then the tree basically ticks each node, checks the result, etc. Each Tree is also a IBehaviorTreeNode so that you can nest trees within trees. If you're worried about repeating logic, you should be able to stick that logic into a static class. However, I have to warn against this just because the reality is you will probably need to customize it per-enemy. It's good enough to have the framework be modular and reusable, if you try to go too deep, you'll drive yourself crazy.
I don't have a ton of implementation detail here because it's a pretty complex subject, as you can imagine.
The most difficult part of any object oriented or design pattern based approach to anything in Unity is that so much of the time if you want any design-time access to the thing you have to make it a MonoBehavior, or you'll end up going crazy with factories.
Abstract classes are possible, but for the Trees, your base class needs to be MonoBehavior. This so you can just stick the tree on the GameObject and tweak values in the Editor.
Anyway, just some things I've learned.
Yeah, shortly after posting this I started researching behavior trees (because I figured that the Angry Ant guy must know what he's doing, but I just don't want to agree to his licensing). I think I understand the basic premise, my only qualm is I'm really confused how to implement. I'm watching an hour long lecture http://www.youtube.com/watch?v=n4aREFb3SsU to try and absorb some of the implementation techniques, but I am a little worried about how to transfer this over to Unity.
I'll have to watch the video you gave me (I only saw the first of three parts of that because I hadn't signed up yet). Thanks for the answer.
Your answer
Follow this Question
Related Questions
Creating an Enemy Jet AI 1 Answer
BDI Agent Programming in Unity3D 1 Answer
need a logical tip for AI 2 Answers
My model from Blender does not follow the AI script properly! 2 Answers
Tutorials for AngryAnt's Behave 1 Answer