Unity--behavior tree

Behavior tree plug-in Baidu Netdisk

Link: https://pan.baidu.com/s/1hshIZPUw4pwv7pg-HZ1cRA 
Extraction code: 4r32 

As the name suggests, a behavior tree is a tree composed of behaviors. It can be applied to the AI ​​of enemies or other objects in the game.

Things to note about behavior trees:

  1. It is executed from top to bottom and from left to right.
  2. Each task has a return value, running, success, fail (conditional tasks are not running)
  3. Each task is a script

After importing unity, you can find the BehaviorDesigner editor in the toolbar.

After opening the editor interface, a brief introduction to each panel

1.Behavior panel

 

2.Tasks panel 

4.Variables panel

There are various variable parameters in the variable panel, which are used to assign values ​​to the behavior tree.

 5.Inspector panel

Of course, the focus of learning a behavior tree is to learn the use of its behavioral task nodes, so this article mainly introduces this content

1. Composites complex tasks (it cannot become a separate task (it must be greater than or equal to a sub-task), it is composed of its sub-tasks)

    1. Sequence, its function is to execute subtasks in sequence from left to right. All subtasks must succeed before success can be returned.

    2. Selector, executes subtasks in sequence from left to right, and will return as long as one subtask succeeds.

2. Decorators (all operate on a single task)

      1.Inverter reverses the result of the character

3. Actions behavioral tasks

  1. Log outputs the text in the log
  2. Movement/Patrol (needs to import the movement package), patrol back and forth (needs to use navemesh, navemesh has been updated in the 2022 version, directly integrating the content of navemeshcomponent into unity, the usage is the same as navemeshcomponent)
  3. Movement/Seek, pursue target target
  4. Movement/Flee, away from the target, where the Fleed Distance is the distance away from the target (that is, when executing the escape task, the distance to the target must exceed this value before the task can return to success, otherwise it will always return to running), and Look Ahead Distance is forward. Seeing distance (understood in this way, when escaping, you move to a certain point behind you. This distance is the distance from a certain point in the escape direction to you now. The larger the point, the farther away it is. If it reaches that point, it will start again. Calculation), note that success will be returned as long as the escape distance is reached (even if the calculated point is not reached, the task will be interrupted, which is in line with the logic of escape)

4. Condition task (used for judgment)

  1. Math/Float Comparison, compare the size of two floating point numbers
  2. Movement/Can See Object (needs to import the movement package), used to determine whether the target object is seen. The target object is the target object, and the return object is the return value (the value returned after seeing the target object, half of which is returned to the target object)

Little knowledge points:

  1. About variable assignment

 

Click the small circle at the back (it is a share type variable in the code) and you can use the variable to assign a value to it.

Can See Objec and Seek and the use of variables

Variables are also divided into local traversal and global variables

Local variables: variables that can be accessed by every task in a behavior tree (that is, they can only be accessed in this behavior tree)

Global variables: variables that can be accessed by each behavior tree (can be accessed by all behavior trees)

You can view the documentation for how to obtain variables using code

2. About the interruption mechanism of Sequence

    1) Interrupting the execution of lower priority means that when a task with a lower priority than itself is executing (you are also making a judgment), if you meet the judgment conditions, you will interrupt the lower priority task and execute yourself.

    2) When you are performing your own tasks, if your tasks do not meet the judgment conditions during execution, your tasks will be interrupted.

    3) both are two fused

3. Handwrite the task methods in the behavior tree by yourself

(1) Create a new script and inherit it from one of the four major tasks. For example, I handwrite a Seek method in Action.

This way you can find the method in the Actions taskbar

Case 1: The enemy is patrolling and pursues the player after discovering it.

Let’s analyze here that the enemy has two states: 1. Patrolling, 2. Pursuing the player.

Case 2: Capture the flag battle, there are flag capturers and flag keepers;

The flag capturer has two states: 1. The flag has not been taken: determine whether there are enemies ahead. If there are enemies, move away from the enemies. If there are no enemies ahead, move the flag closer. 2. The flag is taken: If you have the flag, move towards the end point; if you don’t have the flag, move closer to the flag.

The flag guard has two states: 1. When the flag is not taken: go to his defensive position and chase the enemy if he finds the enemy. 2. When the flag is taken: chase the flag.

There are two behavior trees here, those that get the flag and those that don't get the flag. Therefore, you can use a script to put the behavior trees of the attacker and defender who get the flag in the list, and the same behavior of the attacker and defender who don't get the flag. The tree is also put into another list, so that when the flag is obtained or not obtained, the behavior tree in the corresponding list can be directly activated to switch the purpose of the behavior tree.

Guess you like

Origin blog.csdn.net/qq_62947569/article/details/134914568