unity5利用有限状态机实现AI的圆形,扇形检测算法学习笔记

本文介绍在Unity中使用状态机实现敌方AI的巡逻与追击功能。通过设定PatrolState和ChaseState,敌人能根据玩家位置进行智能行为调整。文章详细展示了如何设置状态机,计算敌人与玩家距离,并根据此距离切换状态。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

先在状态机里面把想要人物的状态设置好,设置一个distance变量在敌人脚本中通过计算敌人与player的距离,来改变distance的值,切换不同的状态,我的代码结构大致如下,状态的行为基本都是放在override public void OnStateUpdate(Animator animator, AnimatorStateInfo stateInfo, int layerIndex) 方法里面的。扇形的话大概也就是在敌人脚本中新增一个最大距离和最大角度,判断主角进入了检测范围内在进行状态机的转换.

这里直接把代码贴一下,防止自己以后忘记

PatrolState: baseTreeState

    private List<Transform> wayPoints = new List<Transform>();
    private int index = 0;
    private GameObject Path;
    // OnStateEnter is called when a transition starts and the state machine starts to evaluate this state
    override public void OnStateEnter(Animator animator, AnimatorStateInfo stateInfo, int layerIndex)
    {
        base.OnStateEnter(animator, stateInfo, layerIndex);
        Path = GameObject.Find("Path");
        foreach (Transform tran in Path.GetComponentsInChildren<Transform>())
        {
            if (tran.gameObject != Path)
            {
                wayPoints.Add(tran);
            }
        }
    }

    // OnStateUpdate is called on each Update frame between OnStateEnter and OnStateExit callbacks
    override public void OnStateUpdate(Animator animator, AnimatorStateInfo stateInfo, int layerIndex)
    {
        Quaternion targetRot =Quaternion.LookRotation(wayPoints[index].position-npc.transform.position,Vector3.up);
        npc.transform.rotation = Quaternion.Slerp(npc.transform.rotation,targetRot, rot * Time.deltaTime);
        npc.transform.Translate(Vector3.forward * speed * Time.deltaTime);
        if (Vector3.Distance(npc.transform.position, wayPoints[index].position)<accuracy)
        {
            index++;
            index %= wayPoints.Count;
        }
    }

ChaseState : baseTreeState


    // OnStateEnter is called when a transition starts and the state machine starts to evaluate this state
    override public void OnStateEnter(Animator animator, AnimatorStateInfo stateInfo, int layerIndex)
    {
        base.OnStateEnter(animator,stateInfo,layerIndex);
        player = GameObject.FindWithTag("Player");
    }


    override public void OnStateUpdate(Animator animator, AnimatorStateInfo stateInfo, int layerIndex)
    {
        Quaternion targetRot = Quaternion.LookRotation(player.transform.position-npc.transform.position);
        npc.transform.rotation = Quaternion.Slerp(npc.transform.rotation, targetRot, rot * Time.deltaTime);
        npc.transform.Translate(Vector3.forward*speed*Time.deltaTime);
    }

 

EnemyAI: MonoBehaviour

    private GameObject player;
    private Animator anim;
    //望向目标
    private void Start()
    {
        player = GameObject.FindWithTag("Player");
        anim = GetComponent<Animator>();
    }

    private void Update()
    {
        anim.SetFloat("distance",Vector3.Distance(transform.position,player.transform.position));
    }

大概效果图就是如下

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

JustEasyCode

谢谢您

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值