[unity plug-in] use BehaviorDesigner plug-in to make AI behavior tree of BOSS

foreword


Behavior Designer is a behavior tree plug-in, a visual editor for planners, programmers, and artists to use conveniently . Behavior Designer provides a powerful API that allows you to easily create tasks (tasks). With
plug-ins such as uScript and PlayMaker, you can easily create a powerful Al system without writing a
line of code. (If you don’t understand PlayMaker, I will introduce it in a separate article later, so you can look forward to it)

The basic use of the BehaviorDesigner plug-in has been introduced in detail by Fa Ge, but I found that there are very few examples, so I have this article. I am thinking about implementing a simple BOSS AI behavior tree, and share the ideas. You These ideas can be further extended

If you don’t understand the basic operation of BehaviorDesigner, you can read this article by Fa Ge: BehaviorDesigner plug-in makes AI behavior tree

material

insert image description here

plug-in

1. AssetStore download
The plugin can be downloaded from AssetStore, address: https://assetstore.unity.com/packages/tools/visual-scripting/behavior-designer-behavior-trees-for-everyone-15277
insert image description here
2. I put 1.7 .1 version to GitCode, address:
https://gitcode.net/unity1/unitybehaviordesigner

1. Basic use

Create a new 2D project, build the environment, the protagonist and the enemy boss, add animator animation to the boss, and roughly connect the lines. Import the
insert image description here
BehaviorDesigner plug-in, click the menu Tools/Behavior Designer/Editor to open the editor window
insert image description here

And create a behavior tree on the enemy, space, add a Repeater node, and check the forever loop
insert image description here
print test, the sequence node controls execution from left to right and
insert image description here
prints the "print log"
insert image description here
running effect every 3 seconds
insert image description here

2. Enemy physical attack

Added script Attack

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

public class Attack : Action
{
    
    
    public Animator animator;
    public GameObject player;
 
    public override void OnStart(){
    
    
    	//切换动画状态为攻击
        animator.SetTrigger("Attack");
    }

    public override void OnEnd(){
    
    
        Debug.Log("打印2");
    }
}

Running effect, the enemy launches an attack every 3 seconds
insert image description here

3. The enemy faces the player

Added FacePlayer script

using BehaviorDesigner.Runtime.Tasks;
using UnityEngine;

//始终面向主角
public class FacePlayer : Action
{
    
    
    private float baseScaleX;
    public GameObject player;
    public override void OnAwake()
    {
    
    
        baseScaleX = transform.localScale.x;
    }

    public override TaskStatus OnUpdate()
    {
    
    
        Vector3 scale = transform.localScale;
        scale.x = transform.position.x > player.transform.position.x ? baseScaleX : -baseScaleX;
        transform.localScale = scale;
        return TaskStatus.Success;
    }
}

Run the effect, first face the player and then attack
insert image description here

4. Enemy Magic Attack

Added Cast script

using UnityEngine;
using BehaviorDesigner.Runtime.Tasks;

public class Cast : Action
{
    
    
    public GameObject player;
    public GameObject spell;

    public override void OnStart(){
    
    
        //生成技能攻击特效
        float playerH = player.gameObject.GetComponent<SpriteRenderer>().sprite.bounds.size.y; //通过SpriteRenderer获得人物高度
        Object.Instantiate(spell, player.transform.position + new Vector3(0, playerH, 0), Quaternion.identity);
    }
}

This time we use the Set Trigger node to control the animation switching
insert image description here
effect. The approximate process is to wait for 3 seconds, face the protagonist, switch the animation to a magic attack action, wait for 0.5 seconds and then switch back to the Idle animation, and run the Cast code to trigger the attack special effect , keeps looping
insert image description here

5. Attack randomly

The key is Random Selectorthe use of nodes

Insert the Random Selector node in the middle, connect the two attack modes respectively, and the running effect
insert image description here

Six, different stages of the enemy

You may also want the boss to have different effects at different stages, such as berserk below half blood, different skills, etc.

Add the IsHealthUnder script, I will give a simple example here

using BehaviorDesigner.Runtime.Tasks;

public class IsHealthUnder : Action
{
    
    
    public float health;

    public override TaskStatus OnUpdate()
    {
    
    
        return health < 50 ? TaskStatus.Success : TaskStatus.Failure;
    }
}

Added Selector node
insert image description here
running effect, because it is a test, I will manually change the enemy's HP here

When the blood volume is less than 50, it is in the second stage, and it will go to the left, and the log will be printed, where you can add new status and skills of the enemy, or cutscenes. When the blood volume is greater than 100, it is in the first stage, and the enemy is
insert image description here
still normal logic
insert image description here

Recommended Learning Videos

If you still don’t understand, I recommend two videos, you can learn by yourself
[Video 1] Use behavior tree to reproduce the fake knight Boss battle in UNITY (Hollow Knight)
[Video 2] Use behavior tree to reproduce in UNITY Current Bumblebee Boss Battle (Hollow Knight)

source code

https://gitcode.net/unity1/unity-ai-behavior

end

Gifts of roses, hand a fragrance! If the content of the article is helpful to you, please don't be stingy with yours 点赞评论和关注, so that I can receive feedback as soon as possible. Every time you write 支持is the biggest motivation for me to continue to create. Of course, if you find something in the article 存在错误or something 更好的解决方法, you are welcome to comment and private message me!

Well, I am 向宇, https://xiangyu.blog.csdn.net

A developer who worked silently in a small company, out of hobbies, recently began to study unity by himself. If you encounter any problems, you are also welcome to comment and private message me. Although I may not necessarily know some problems, I will check the information of all parties and try to give the best suggestions. I hope it can help more people who want to learn programming People, encourage each other~
insert image description here

Guess you like

Origin blog.csdn.net/qq_36303853/article/details/132540936