最近公司项目遇到一个需求:要求AI人物具备寻路、巡逻、攻击、躲避、逃跑等行为。
FSM AI Template这款插件就挺符合的。有个老哥的好像也挺不错的,但是由于时间紧就没仔细研究了。
插件的官网链接
unity商店里面也有,可以自行去搜。但是这个插件的教程是真的少(国内直接找不到教程,最多找到资源),只有在油管上才有,而且是作者自己出的。
巡逻:巡逻跟着官网的弄就可以了,
需求1:发现目标的时候把动画切换为某一个动画片段,丢失目标之后再把动画切换为原来的动画片段。
需求2:更改当前AI的检测范围(Min~Max)以及可以检测的layer和tag(DetectLayer和DetectTag的意思是AI只能够检测到具有这些属性的物体/AI)
要达到检测到就攻击的效果还需要把这个也改一下。
需求3:给当前AI指定一个路径数组(达到巡逻效果)
路径点需要在这创建,弄好后赋予过去就可以了。
需求4:更换AI的枪械。
(声音、特效、子弹、子弹发射的位置什么的具体得自己去这个属性面板具体修改)
需求5:创建一个新的AI(我这里是随便创建的)
需求6:判断当前AI到达了第几个点。
using Invector.vCharacterController.AI;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class CurrentWaypoint : MonoBehaviour
{
private vControlAIShooter vControlAIShooter;
string name;
bool isTrue;
void Start()
{
isTrue = true;
vControlAIShooter = GetComponent<vControlAIShooter>();
name = transform.name;
}
// Update is called once per frame
void Update()
{
Debug.Log(vControlAIShooter._currentWaypoint);
if (name == "redPatrolAI" && isTrue)
{
if (vControlAIShooter._currentWaypoint >= 3)//如果该AI到达第三个点就执行这里面的逻辑
{
//AIKongZhi.instance.blueAI.SetvWaypointArea("blueNotPatrol0", 0);
//AIKongZhi.instance.blueAI.SetvWaypointArea("blueNotPatrol1", 1);
//AIKongZhi.instance.blueAI.SetvWaypointArea("blueNotPatrol2", 2);
//AIKongZhi.instance.blueAI.SetvWaypointArea("blueNotPatrol3", 3);
isTrue = false;
}
}
}
}
目前用到的就这么多,等后面还有其他的再添加。
如有不对的地方欢迎指正!