行为树 和 Behavior Designer

对于游戏AI,行为树是个简单可扩展的解决方案。

本文简单介绍 行为树 以及 Unity 的 Behavior Designer 插件。

行为树

行为树(Behavior Tree,简称 BT),是由行为节点组成的树状结构。

行为树中的节点,会在某一帧中被调用,然后立即且必须得到以下之一的结果:成功 Success、失败 Failure、运行中 Running。然后根据返回值进行下一步操作。

行为树会自顶向下的,根据节点定义的规则和节点返回值来搜索这颗树,最终确定需要执行的行为节点。

节点不需要维护向其他节点的转换, 因此拥有更好的封装性和模块性,让游戏逻辑更加直观。

通过 Behavior Designer 插件 了解行为树

Behavior Designer 官网: http://www.opsive.com/assets/BehaviorDesigner/

Behavior Designer 官网文档: http://www.opsive.com/assets/BehaviorDesigner/documentation.php

官方文档的介绍:Behavior Designer is a behavior tree implementation designed for everyone - programmers, artists, designers. Behavior Designer offers an intuitive visual editor with a powerful API allowing you to easily create new tasks. It also includes hundreds of tasks, PlayMaker integration, and extensive third party integration making it possible to create complex AIs without having to write a single line of code!(大意就是说 Behavior Designer 这个行为树插件,提供了方便使用的可视化编辑器,他提供的强大api, 配合 PlayMaker 等插件,可以在不写代码的情况下构建复杂的 AI )

Behavior Designer 中存在四种节点:
- Actions 动作节点 : 官方文档
- Composites 组合节点 : 官方文档
- Conditionals 条件节点 : 官方文档
- Decorators 修饰节点 : 官方文档

Behavior Designer 中基本操作是用 Decorators 和 Composites 搭逻辑框架,然后自定义 Conditionals 来实际判断和执行 Actions。

下面看一个具体的例子:
  1. 导入 Unity:可以在官网或者在 Unity Asset Store 中下载 package 文件。

  2. 查看 Editor 面板:导入 Behavior Designer 后,可以在 菜单栏/Tools/Behavior Designer/Editor 打开编辑器。

  3. 窗口 Tab 介绍

    • Behavior: 可以设置行为树的名子和属性
    • Tasks: 所有 Conditional(条件)和 Action(动作)的列表(包括内置条件、动作和自定义条件和动作)
    • Variables: 公共变量窗口,具体见后面介绍。
    • Inspector: 行为树节点的Inspector,是针对某个节点的详细属性。设置参数和绑定变量用。
  4. 添加一个 Behavior Tree:在 Hierarchy 窗口选中一个 GameObject,在刚刚打开的 Behavior Designer 窗口右键, 点击 Add Behavior Tree, 如果看到 GameObject 被添加了一个 Behavior Tree 脚本,说明添加成功。

  5. 设计一个士兵的AI逻辑:

    • 士兵能够发现距离5以内的敌人;
    • 士兵总是发现距离最近的敌人;
    • <
  • 4
    点赞
  • 16
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值