当前位置:网站首页>A brief introduction to the behavior tree of unity AI
A brief introduction to the behavior tree of unity AI
2022-07-05 03:34:00 【Xiankui xan】
1. Behavior tree birth
Designing games AI When , Our goal is to find a simple , The scheme of extensible editing logic , So as to accelerate the iteration speed of game development .
Take soldiers as an example , Suppose soldiers have Free 、 fighting 、 escape Three states , State machine (FSM) Is the first plan to come to mind , Then the state machine diagram of soldiers can be shown as follows .
But as the development goes on , State one more , State machine maintenance is not so easy , State machine The transition line between them is like a runaway horse , Can't control . For example, add another hide and return to the starting state , Then the connection will become more and more complex .
So the predecessors designed Behavior tree Abstract to solve this problem .
2. What is a behavior tree ?
Behavior tree , English is Behavior Tree, abbreviation BT, It's a tree for controlling AI Decision making behavior 、 A tree structure containing hierarchical nodes .
2.1 principle : Traverse
When we have to decide what kind of behavior this soldier is going to do , We'll be top-down , Search the tree by some criteria , Finalize what needs to be done ( Leaf nodes ), And execute it , This is the basic principle of behavior tree .
2.2 form
The behavior tree is mainly abstracted from the following four nodes Combination of node 、 Decoration node 、 Conditional node 、 Behavior node .
① Combination of node (Composites)
It mainly includes :Sequence Sequence condition ,Selector Choose the conditions ,Parallel Parallel conditions and the conditions of their combination .
② Decorated nodes (Decorator)
Branches connecting leaves , It is all kinds of decoration nodes , These nodes determine AI How to start from the top of the tree according to different situations , To follow different paths to the final leaf .
For example, let the child nodes operate circularly (LOOP) Or let the son task Run until it returns a certain running state value (Util), Or will task Negative return value of (NOT) wait
③ Conditional node (Conditinals)
Used to judge whether a condition is true . So far , yes Behavior Designer In order to implement the principle of single responsibility , Treat the judgment as a node independently , For example, judge whether a target is in the field of vision , In fact, they are attacking Action It can also be written in it , But this way Action It's not single , It is not conducive to the reuse of visual field judgment processing . The general condition node appears in Sequence In the control node , Followed by Action node .
④ Behavior node (Action)
Behavior node is the node that really does things , The behavior node is at the end of the tree , It's all leaf nodes , That's all. AI Orders to actually do things ;
Drive the behavior of the character by using the association between the nodes in the behavior tree , Rather than telling a character what to do with specific code , It's much more interesting to come , This is also the most exciting point of the behavior tree . In this way, we only need to abstract good behavior , Don't care what happened in the battle .
3. Tree traversal -Tree Traversal
One characteristic of the behavior tree is , It will “ One layer at a time ” Check the nodes in turn , And each floor needs to spend one tick Time for , So it takes several tick To complete the process from the top to the bottom , To complete its logic , This is very different from using code to realize functions .
This is not a very efficient way , Especially when your tree gets very deep . I think the implementation of behavior tree must have the ability to be in a tick Complete the judgment logic of the whole behavior tree .
4. workflow -Flow
The behavior tree consists of many different types of nodes , They all have a common core function , That is, they will return one of the three states as a result . These three states are :
- success -Success;
- Failure -Failure;
- Running -Running;
The first two , Just like their name , It is used to inform their parent node of the success or failure of the operation . The third means that it is still running , The result has not been decided yet , The next tick Check the operation results of this node when you need to .
This function is very important , It can keep a node running for a period of time to maintain some behavior .
For example, a “walk( walk )” The node will continue to return during the process of calculating pathfinding and keeping the character walking “Running” To keep the character in this state .
If pathfinding fails for some reason , Or except for some conditions that make the walking behavior have to stop , Then this node will return “Failure” To tell its parent node ;
If the character goes to the designated destination , Then the node returns “Success” To indicate that the walking command has been successfully completed .
These states can be used to determine the direction of the behavior tree , Make sure AI The behaviors in the behavior tree can be executed in some order in the way we expect .
See the article at the end of the article for specific introduction , Here is just a record and introduction
Data context -Data Context
When a behavior tree is called , A data context is also created .
Its purpose is to store variables interpreted and changed by nodes . These variables can then be read and written by the node in combination with the context of the data , So that the whole behavior tree remains a unified whole .
Behavior tree description :
Behavior trees are various classic control nodes + Combine behavior nodes , So as to realize the control of some complex behavior states , The behavior tree plug-in used here is Behavior Designer, It contains four types of nodes . Namely action node 、 Combination of node 、 Condition node and decoration node .
(1) Composites Combination of node , It mainly includes :Sequence Sequence condition ,Selector Choose the conditions ,Parallel Parallel conditions and the conditions of their combination .
(2) Decorator Decoration node , This node contains only one child node , There are no special functions added to this child node , For example, let child nodes operate circularly or let children task Run until it returns a certain running state value , Or will task And so on
(3) Actions Behavior node , Behavior node is the node that really does things , It is a leaf node .Behavior Designer The plug-in comes with many Action node , If not enough , You can also write your own Action. For the behavior state written by yourself , Generally, you need to add your own
(4) Conditinals Conditional node , Used to judge whether a condition is true . So far , yes Behavior Designer In order to implement the principle of single responsibility , Treat the judgment as a node independently , For example, judge whether a target is in the field of vision , In fact, they are attacking Action It can also be written in it , But this way Action It's not single , It is not conducive to the reuse of visual field judgment processing . The general condition node appears in Sequence In the control node , Followed by Action node .
Behavior Designer The sharing of variables is handled as follows :
In the same Behavior Tree( Generally one GameObject There is one Behavior Tree) Of Task Local variables shared between can be directly in the editor Variables add to ; In addition, it also supports in different Behavior Tree Global variables shared between ; And support Task And non Task( Other scripts in the game system ) Pass variables between , Through the following code :
behaviorTree.GetVariableName(“MyVariableName”);
behaviorTree.SetVariableName(“MyVariableName”, value);
Write your own behavior subclasses , You can create nodes with customized behavior , These nodes need to inherit one of the four nodes . Such as , We can create a condition node for monster patrol , That is, the monster defaults to patrol , When the distance between the player and the monster is less than a certain distance , Monsters start chasing players .
Reference link :
Wu Yifeng : game AI - Behavior tree Part1: brief introduction
边栏推荐
- 端口,域名,协议。
- Daily question 2 12
- Three line by line explanations of the source code of anchor free series network yolox (a total of ten articles, which are guaranteed to be explained line by line. After reading it, you can change the
- Port, domain name, protocol.
- 【软件逆向-分析工具】反汇编和反编译工具
- Why are there fewer and fewer good products produced by big Internet companies such as Tencent and Alibaba?
- 【做题打卡】集成每日5题分享(第三期)
- glibc strlen 实现方式分析
- Dart series: collection of best practices
- 线程基础知识
猜你喜欢
The perfect car for successful people: BMW X7! Superior performance, excellent comfort and safety
The perfect car for successful people: BMW X7! Superior performance, excellent comfort and safety
2. Common request methods
Pdf things
Tencent cloud, realize image upload
[positioning in JS]
单项框 复选框
[learning notes] month end operation -gr/ir reorganization
Port, domain name, protocol.
Azkaban installation and deployment
随机推荐
Kubernetes - identity and authority authentication
What is the most effective way to convert int to string- What is the most efficient way to convert an int to a String?
[positioning in JS]
qrcode:将文本生成二维码
[groovy] loop control (number injection function implements loop | times function | upto function | downto function | step function | closure can be written outside as the final parameter)
New interesting test applet source code_ Test available
Easy processing of ten-year futures and stock market data -- Application of tdengine in Tongxinyuan fund
Pat grade a 1119 pre- and post order traversals (30 points)
Yyds dry goods inventory embedded matrix
Blue Bridge Cup single chip microcomputer -- PWM pulse width modulation
【做题打卡】集成每日5题分享(第三期)
[groovy] string (string type variable definition | character type variable definition)
2021 Li Hongyi machine learning (3): what if neural network training fails
Pat class a 1162 postfix expression
ICSI213/IECE213 Data Structures
[system security] ten thousand words summary system virtualization container bottom layer principle experiment
Azkaban实战
About MySQL database connection exceptions
this+闭包+作用域 面试题
Qrcode: generate QR code from text