添加野猪追逐状态
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
标签:状态,转换,void,localScale,transform,追击,currentEnemy,override,public From: https://www.cnblogs.com/hellozjf/p/18032458