3-5. 追击状态的转换

添加野猪追逐状态

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class BoarChaseState : BaseState
{
    public override void OnEnter(Enemy enemy)
    {
        currentEnemy = enemy;
        // Debug.Log("Chase");
        currentEnemy.currentSpeed = currentEnemy.chaseSpeed;
        currentEnemy.lostTimeCounter = currentEnemy.lostTime;
        currentEnemy.anim.SetBool("run", true);
    }

    public override void LogicUpdate()
    {
        if (currentEnemy.lostTimeCounter <= 0)
        {
            currentEnemy.SwitchState(NPCState.Patrol);
        }

        if ((!currentEnemy.physicsCheck.isLeftGround) ||
            (!currentEnemy.physicsCheck.isRightGround) ||
            (currentEnemy.physicsCheck.touchLeftWall && currentEnemy.faceDir.x < 0) ||
            (currentEnemy.physicsCheck.touchRightWall && currentEnemy.faceDir.x > 0))
        {
            // 追逐状态立马转身
            currentEnemy.transform.localScale = new Vector3(-1 * currentEnemy.transform.localScale.x, currentEnemy.transform.localScale.y, currentEnemy.transform.localScale.z);
        }
    }

    public override void PhysicsUpdate()
    {

    }

    public override void OnExit()
    {
        currentEnemy.anim.SetBool("run", false);
    }
}

发现玩家的时候立马进入追逐状态

FoundPlayer 的原理是向前投射一个 Box,期间撞到 Player 的话就会返回 true

另外当玩家离开野猪视野范围的时候,需要让野猪在一段时间后恢复巡逻状态

项目相关代码

代码仓库:https://gitee.com/nbda1121440/2DAdventure.git

标签:20240225_1441

posted @ 2024-02-25 15:23  hellozjf  阅读(39)  评论(0编辑  收藏  举报