Unity BehaviorDesigner behavior tree summarizing

BehaviorDesigner-- behavior tree logic for controlling AI, like this:

 

This behavior is above the tree realized this logic:

When there is an object in accordance with Input Input to move, find the nearest available target when no Input, found the target print out; if neither Input did not find a target, it has been in the Idle state.

 

Here is a summary of the most common basics BehaviorDesigner:

It must first clear a behavior tree must have an object according to Fu, which is the interpretation of a series of behavior patterns of an object.

These behavior patterns composed Task, each executable is a block diagram of the Task, these Task connected in the logic design would comprise the behavior of the object tree.

Behavior tree starting from the root, from top to bottom, from left to right to perform under each Task, Task executed any will return to a state when the root Task returns a success (or failure) status, meaning the behavior of a single tree execution ends.

 

Task the following states:

Is being performed, successful execution, failed execution, is not activated.

Each Task must be in one of these types of state.

 

 

Task divided into different types, each type of action Task vary substantially essential to recognize the role of each Task class:

 

Composites (composite type): This type of Task mainly to control the behavior of the tree, up to the most important class is used, a relatively complex behavior of any tree contain such Task, but it does not do any specific behavior, they the following generally requires child nodes to perform specific operations.

Decorators (decoration): Sub Task thereof used for performing additional operations at, e.g. inversion results, and the like repeatedly performed.

Actions (behavior classes): specific categories to perform an operation, the implementation of the Task single frame may not be able to complete. A large number, generally located the behavior of the tree leaf nodes, each one did not need to figure out, because you can easily expand their type of Task. Specific details on how the back will expand.

Conditionals (condition category): General placed before the Class Action Task constrain, continue down only when conditions are satisfied (or not satisfied), completed only one judge in a single frame.

 

The most basic of the most commonly used are the following two composite class Task:

Selector (selection): Or the equivalent of the operation, the following sub-Task long as there is a return to the success it returns success only if all of them failed to return before returning failure.

Sequence (sequence): And the equivalent of the operation, the following sub-Task failed to return as long as there is a failure it returns only if all of them return a success before returning success.

The remaining only on the basis of both the deformation, for example, while the sub-Task execution random order or the like, can see specifically described.

 

Composite class Task priority and interrupted:

This is very important, it is the only special properties of the composite class Task: There are four - do not interrupt can be interrupted by itself, can break below the other Task Task priority can not only break itself can also play off below its priority.

 

It can be understood as the Task jump or restart. For example, the top figure, the object can be interrupted by Input move it lower priority than Task find the enemy and the Idle state, find the enemy can interrupt the Idle state, which means that when the object is in the Idle state if the enemy succeeded in finding , then it becomes attack when attacking enemies input received instruction time and to jump to move only when neither the input command input and does not find the enemy, only to Idle, once some point higher than its priority Task meet the pre-conditions, it will jump higher execution priority.

Therefore, in the design tree behavior, generally the higher priority behavior Task tree to the left, a low priority is placed on the right side, because this side and can not interrupt a higher priority than that of the Task.

After opening the Task Abort Type upper-left corner of the box will appear to the right or down arrow as a symbol of tips.

 

Custom Task task:

Task general compound class and the decoration is enough, and even some never use, and specific behaviors and conditions like Task Class Task could never meet our needs, and you can write your own simplify such a large degree of Task the behavior of the entire tree structure.

Step Task wrote as follows:

1. The introduction of the namespace:

using BehaviorDesigner.Runtime;
using BehaviorDesigner.Runtime.Tasks;

2. clear inheritance Task type:

public class MyInputMove : Action
public class MyIsInput : Conditional

3. Task execution process known within the function :( can see the official document, written very clearly, here the most important of a map Tieshanglai)

 

Unity and writing scripts found similar, not the same place where there is Update return value, to return the state to perform the task, only when the execution of each frame in the Running state.

using UnityEngine;
using BehaviorDesigner.Runtime;
using BehaviorDesigner.Runtime.Tasks;

public class MyInputMove : Action
{
    public SharedFloat speed = 5f;
    public override TaskStatus OnUpdate()
    {
        float inputX = Input.GetAxis("Horizontal");
        float inputZ = Input.GetAxis("Vertical");
        if (inputX != 0 || inputZ != 0)
        {
            Vector3 movement = new Vector3(inputX, 0, inputZ);
            transform.Translate(movement*Time.deltaTime*speed.Value);
            return TaskStatus.Running;
        }
        //return TaskStatus.Success;
        return TaskStatus.Failure;
    }
}

There is a difference, is encapsulated in a layer of Share Task type attribute, and how is it different types of non Share it?

Here is a comparison:

using UnityEngine;
using BehaviorDesigner.Runtime.Tasks;
using BehaviorDesigner.Runtime;

public class MyLog : Action
{
    public string staticLog;
    public SharedString shareLog;

    public  override TaskStatus OnUpdate ()
    {
        Debug.Log(staticLog + shareLog.Value);
        return TaskStatus.Success;
    }
}

 

 

 

You can easily see that the variable type Share Here is a behavior that can be passed in the tree, because it is not convenient to directly call and modify variables, how to do it between Task, so they increase a type of Share variable exchange between the various Task behavior tree.

For example, here, each found the nearest enemy is not the same, to carry out an attack or to print the results based on the value of the next Task Task on a return, then a fixed property will not be able to meet the requirements, but direct calls to other Task coupling added, so he alone Share variable transmission. Which would also facilitate unified management.

Share another type of variable can also increase their global and local is the difference between a behavior has all the tree, a tree only has.

The above is to find the name of the nearest enemy and return, you can take directly to the value returned here when other Task such as printing.

using UnityEngine;
using BehaviorDesigner.Runtime;
using BehaviorDesigner.Runtime.Tasks;

public class MyFindLatestTarget : Action
{
    public SharedGameObjectList origins;
    public bool bUseTag = false;
    public SharedString tag;
    public SharedGameObject result;
    public SharedString resultName;

    public override void OnStart()
    {
        if (bUseTag)
        {
            origins.Value.Clear();
            origins.Value.AddRange(GameObject.FindGameObjectsWithTag(tag.Value));
        }
    }
    public  override TaskStatus OnUpdate ()
    {
        if (origins.Value.Count > 0)
        {
            float minDistance=Mathf.Infinity;
            GameObject minDistanceObj=null;
            foreach(var item in origins.Value)
            {
                if (item == null)
                    continue;
                float sqrDistance = (item.transform.position - transform.position).sqrMagnitude;
                if (sqrDistance < minDistance)
                {
                    minDistance = sqrDistance;
                    minDistanceObj = item;
                }
            }
            result.Value = minDistanceObj;
            resultName.Value = minDistanceObj.name;
            return TaskStatus.Success;
        }
        return TaskStatus.Failure;
    }
}

When the variable takes a value Share need .Value.

 

Guess you like

Origin www.cnblogs.com/koshio0219/p/11734423.html