Unity 使用AI插件制作简单平面2DAI敌人

 


前言

利用 NavMeshPlus 搭配 Unity自带的AI Navigation制作俯视角2D敌人,我拿我制作的一款2D坦克对战的游戏举例子,先看视频,实现了自动寻路,释放子弹躲避子弹的功能。

项目4

 

一、准备工作

NavMeshPlus下载链接:GitHub - h8man/NavMeshPlus: Unity NavMesh 2D Pathfinding

AI Navigation在unity package中直接下载即可

3D游戏应该很多人都使用过AI Navigation寻路,我也拿这个用在2D上用了一下,也就是在2d创建一个3d平面做地面,但是在手机上运行总会出现莫名其妙的bug,后来利用Plus才能够AI才能正常运作。

二、使用步骤

1.建立可行走区域地图

首先确保一个物体上挂载了Sprite Render,3D游戏是需要MeshRender,这个sprite的形状就是地图的形状,我测试了包括不规则图形也可以渲染出正确的可行走区域。首先在这个物体上挂载下面这几个组件,Navigation Modifier是用来标记这是可走区域还是不可走区域,默认可走区域,可以把响应的障碍物设置为不可走区域

d573a98ee20443cbbcabe5935cfc14f4.png

2.渲染寻路地图

新建一个空物体,这个空物体有以下几个组件,注意名字不要弄错了,这里的组件是Plus中的插件和Unity自带的很像,第三个组件是2D专有的组件,加入后点击Roate surface to xy,这个一定要先点击,点完后,点击Bake,你会发现地图图片上出现了蓝色区域,那就是AI可寻路的区域

fca8df6e727b47c49fc3316813715a26.png3ff920471c494a34ab0056a0df6439b1.png

 3.制作敌人AI

对于AI的制作和unity插件做3D寻路差别不大,首先AI挂上组件,至于每个属性的作用,大家可以自己尝试摸索

58dcdeb7e1094e0d9587e73784fa9cb2.png

接着就是写脚本了,有以下几个重要的知识点

1.navMeshAgent.Warp(fuhuodian.position);如果要改变AI初始生成的位置建议使用Warp函数

navMeshAgent是获取的自身Agent组件

2.    navMeshAgent.SetDestination(target.position);利用这个方法设置目标位置,如果目标位置一直在变化,需要一直调用这个方法

3. navMeshAgent.isStopped = true; 利用这行代码控制AI是否继续移动

 4.躲避障碍物

要让AI自动躲避移动的障碍物,只需要在障碍物身上添加下面这个组件

38a23fe5b9af4b9dac1d2334f53cd193.png

  5.扩展思考

   关于AI的逻辑操作,我使用了射线检测,如果AI和玩家直线区域没有墙壁就停止寻路发射子弹

如果有墙壁就继续寻路。并且根据速度方向改变AI旋转方向

        我做的AI他在躲避子弹这上面还有待提升,因为躲避障碍物的同时,AI还依然在寻路找敌人,这就AI可能出现矛盾鬼畜。。

 


总结

这种方法应该是最方便快捷的方法了,大家感兴趣可以自己探索这个插件更深处。如果想体验我做的坦克与人机对决,可以在taptap搜指尖坦克体验。

 

 

This extension requires one license per seat Requires Unity 4.6.0 or higher. Behavior trees are used by AAA studios to create a lifelike AI. With Behavior Designer, you can bring the power of behaviour trees to Unity! Behavior Designer is a behaviour 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 was designed from the ground up to be as efficient as possible with zero allocations after initialization. As a result, it runs great on all platforms including mobile. Behavior Designer is dedicated to behavior trees, which means that each update will be a better behavior tree implementation. Features: - An intuitive visual editor - A powerful API - Visual runtime debugger - Variables to communicate between tasks - Conditional Aborts - Built in event system - Unity 5 multiplayer support - Use existing code with reflection tasks - Hundreds of tasks - Evaluate tasks using Utility Theory - Realtime error detection - Binary or JSON serialization - Data-oriented design - Zero runtime allocations after startup - Object drawers (property drawers) - Includes runtime source code - Extensive documentation and videos - Sample projects available online - And more Addon Packs: - Formations Pack - Movement Pack - Tactical Pack Complete Projects: - Deathmatch AI Kit Third Party Integrations: - 2D Toolkit - A* Pathfinding Project (Movement Pack) - Adventure Creator - Anti-Cheat Toolkit - Apex Path (Movement Pack) - Blox - Camera Path Animator - Chronos - Cinema Director - Control Freak - Core GameKit - Curvy - Dialogue System - DOTween - Final IK - Glow Effect - ICode - Inventory Pro - LeanTween - Love/Hate - Master Audio - NGUI - Particl
This extension requires one license per seat Requires Unity 4.6.0 or higher. Behavior trees are used by AAA studios to create a lifelike AI. With Behavior Designer, you can bring the power of behaviour trees to Unity! Behavior Designer is a behaviour 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 integ ration making it possible to create complex AIs without having to write a single line of code! Behavior Designer was designed from the ground up to be as efficient as possible with zero allocations after initialization. As a result, it runs great on all platforms including mobile. Behavior Designer is dedicated to behavior trees, which means that each update will be a better behavior tree implementation. Features: - An intuitive visual editor - A powerful API - Visual runtime debugger - Variables to communicate between tasks - Conditional Aborts - Built in event system - Unity 5 multiplayer support - Use existing code with reflection tasks - Hundreds of tasks - Evaluate tasks using Utility Theory - Realtime error detection - Binary or JSON serialization - Data-oriented design - Zero runtime allocations after startup - Object drawers (property drawers) - Includes runtime source code - Extensive documentation and videos - Sample projects available online - And more Addon Packs: - Formations Pack - Movement Pack - Tactical Pack Complete Projects: - Deathmatch AI Kit Third Party Integrations: - 2D Toolkit - A* Pathfinding Project (Movement Pack) - Adventure Creator - Anti-Cheat Toolkit - Apex Path (Movement Pack) - Blox - Camera Path Animator - Chronos - Cinema Director - Control Freak - Core GameKit - Curvy - Dialogue System - DOTween - Final IK - Glow Effect - ICode - Inventory Pro - LeanTween - Love/Hate -
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值