public override void Update(List<ICharactor> targets) { //如果目标为空或者死亡,切换回待机模式 if (_chasetarget == null || _chasetarget.BKilled) { CharactorAi.ChangeAiState(new IdleAIState()); return; } //如果目标在可攻击的范围内,切换到攻击模式 if (CharactorAi.TargetRange(_chasetarget, 2)) { CharactorAi.StopMove(); CharactorAi.ChangeAiState(new AttackAIState(theAttackTarget: _chasetarget)); return; } //追赶的目标距离过远,回到待机 if (!CharactorAi.TargetRange(_chasetarget, 15)) { CharactorAi.ChangeAiState(new IdleAIState()); } CharactorAi.Moveto(_chasetarget.transform.position); }
public override void Update(List <ICharactor> targets) { Vector3 nowPosition = CharactorAi.GetPosition(); ICharactor theNearTarget = null; float minDist = 999f; foreach (var target in targets) { if (target != null) { if (target.BKilled) { continue; } float dist = Vector3.Distance(nowPosition, target.transform.position); if (dist < minDist) { minDist = dist; theNearTarget = target; } } } Debug.Log(theNearTarget); if (theNearTarget == null) { return; } //如果检测到目标在攻击范围内,则切换到攻击状态 if (CharactorAi.TargetRange(theNearTarget, 2)) { CharactorAi.ChangeAiState(new AttackAIState(theAttackTarget: theNearTarget)); } else //如果不在攻击范围内则切换到追赶状态 { //在有效视野内才可以进行锁定追击 if (CharactorAi.UsefulView(theNearTarget) < 90) { CharactorAi.StopMove(); CharactorAi.ChangeAiState(new ChaseAiState(theTarget: theNearTarget)); } } }