Unity Advanced: behavior tree

Copyright notice:

  • This article Original starting in the following website:
  1. Blog Park "excellent dream maker culture" in space: https: //www.cnblogs.com/raymondking123
  2. Excellent dream maker culture official blog: https: //91make.top
  3. Excellent game dream maker culture lecture: https: //91make.ke.qq.com
  4. "Excellent dream maker culture" of micro-channel public number: umaketop
  • You are free to reprint, but must include the full copyright notice

Plug behavior tree

Advantage: the soldier can not only move along a fixed path animation, you can also move along the specified path point, the process of other acts (attack, defense) can also travel

Interrupt type
Lower Priority: the priority of the right to interrupt the implementation of
Self: only interrupt its operation, determination, only a beginning can see object determine the successful execution seek to see the enemy, if the start straight seek not judged, back in time see the enemy can not be tracked because they do not determine
Both: will be executed

MOVEMENT {
Cover cover
Evade avoid
Flee flee
Flock gathered
Leader follow follow the movement
Move towards moving in one direction
Patrol patrol
Pursue chase
Queue one by one by
Search Search
Search the Seek
Wander strolling
}
ACTIONS {
the Log output text
Idle stop for some time
Wait wait time task
}
Composites {
sequence executed from left to right, the successful implementation of subtask before returning successful, returns a failed task failed
Selector sequentially performed later is not performed successfully
}
Coditionals / Basic / Mathf / Comparision the Float: determining a size of the first and second number , returns to true, false
movement NC Joining Module / PATROL: around a little exercise
movement / Seek: control reaches the target point
Can see Object: to see things

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

//这个任务脚本的作用就是控制游戏物体达到目标为止
public class MySeek :Action{//这个任务的调用是behacior designe行为树控制
    public SharedTransform target;//这是我们要达到的目标位置
    //public float speed;
    public SharedFloat sharedSpeed;
    //public float arriveDistance = 0.1f;
    public SharedFloat sharedArriveDistance=0.1f;
    private float sqrArriveDistance;

    public override void OnStart()
    {
        sqrArriveDistance = sharedArriveDistance.Value * sharedArriveDistance.Value;
    }
    //当进入到这个人物的时候,会一直调用这个方法,一直到任务结束,你返回一个成功或者失败的状态,那么任务结束如果返回一个running状态,那这个方法会继续调用
    public override TaskStatus OnUpdate()
    {
        //这个方法的调用频率,默认是跟unity里面的帧保持一致的
        if (target == null||target.Value==null)//判断target是否有值
        {
            return TaskStatus.Failure;//失败状态
        }
        transform.LookAt(target.Value.position);//直接朝向目标位置
        transform.position = Vector3.MoveTowards(transform.position, target.Value.position, sharedSpeed.Value * Time.deltaTime);
        if((target.Value.position - transform.position).sqrMagnitude < sqrArriveDistance)
        {
            return TaskStatus.Success;//如果距离目标位置的距离较小,人未到达了目标位置,直接return成功
        }
        return TaskStatus.Running;//继续执行
    }
}

Definition of speed, distance, position to be reached. Determining whether the operation can be calculated from the target itself, moved to the target, the target to reach the end point.

  public Transform[] targets;//判断是否在视野内的目标
    public float fieldOfViewAngle = 90;
    public SharedFloat sharedViewDistance;
    public  SharedTransform target;//共享的变量

    public override TaskStatus OnUpdate()
    {
        if (targets == null) return TaskStatus.Failure;
        foreach(var target in targets)//遍历每一个目标是否满足调用
        {
            float distance = (target.position - transform.position).magnitude;//求距离目标减去当前自身坐标球的距离
            float angle = Vector3.Angle(transform.forward, target.position - transform.position);//求向量夹角,前方向减目标与主角之间的向量
            if (distance < sharedViewDistance.Value && angle < fieldOfViewAngle * 0.5f) {//夹角小于视野的一半,距离比视野距离小
                this.target.Value = target;//共享变量赋值
                return TaskStatus.Success;//成功

            }
        }
        return TaskStatus.Failure;//失败
    }

Determining the distance, it is within the field of view (field of view angle of less than half)

Guess you like

Origin www.cnblogs.com/raymondking123/p/11410729.html