追击移动从 A* 寻路(MoveTo)改为直接朝玩家水平爬行(MoveInDirectionWithSpeed): 地面爬行怪不需寻路,且不依赖场景 NavGraph 烘焙(PathBerserker2d 在测试场景失效)。 ENM_CaoZhi prefab 补挂 EnemyAiBrain(_definitionId=E001),使对象池/prefab 生成的敌人也有AI (此前只有脚手架场景实例有brain)。PlayMode 验证:敌人爬向玩家并接触伤害击杀。 Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
61 lines
2.1 KiB
C#
61 lines
2.1 KiB
C#
using System.Collections;
|
|
using Animancer;
|
|
using UnityEngine;
|
|
|
|
namespace BaseGames.Enemies.Abilities
|
|
{
|
|
/// <summary>
|
|
/// 循环追击 + 体接触伤害能力(纯执行器)。
|
|
/// 每帧 MoveTo 追向玩家、开接触伤害;**何时停止由 AI 决策层负责**——AI 在"脱离视野"时
|
|
/// 中断本能力(AI decides when to stop, ability implements the chase)。
|
|
/// 可复用于任何需要"追击+接触伤害循环"的敌人。
|
|
/// </summary>
|
|
public class ContactChaseAbility : EnemyAbilityBase
|
|
{
|
|
[Header("动画")]
|
|
[SerializeField] private ClipTransition _loopClip;
|
|
[SerializeField] private ClipTransition _endClip;
|
|
|
|
[Header("接触伤害")]
|
|
[SerializeField] private BodyContactDamage _contactDamage;
|
|
|
|
protected override IEnumerator ExecuteCoroutine()
|
|
{
|
|
Phase = AbilityRunState.Active;
|
|
|
|
if (_loopClip.Clip != null)
|
|
_animancer.Play(_loopClip);
|
|
|
|
if (_contactDamage != null)
|
|
_contactDamage.enabled = true;
|
|
|
|
// 追击直到玩家消失或被 AI 中断(退出追击的决策归 AI,见 EnemyBrainContext.InterruptAbilities)。
|
|
// 地面爬行怪用直接水平爬行追击(朝玩家方向),不依赖 A* 寻路。
|
|
float runSpeed = _enemy.Stats != null ? _enemy.Stats.RunSpeed : 0f;
|
|
while (_enemy.PlayerTransform != null)
|
|
{
|
|
float dir = _enemy.PlayerTransform.position.x >= _enemy.transform.position.x ? 1f : -1f;
|
|
_enemy.MoveInDirectionWithSpeed(dir, runSpeed);
|
|
yield return null;
|
|
}
|
|
|
|
CleanupChase();
|
|
|
|
if (_endClip.Clip != null)
|
|
{
|
|
_animancer.Play(_endClip);
|
|
yield return EnemyAbilityWaits.Get(_endClip.Clip.length);
|
|
}
|
|
}
|
|
|
|
protected override void OnInterrupted(InterruptReason reason) => CleanupChase();
|
|
|
|
private void CleanupChase()
|
|
{
|
|
if (_contactDamage != null)
|
|
_contactDamage.enabled = false;
|
|
_enemy.StopMovement();
|
|
}
|
|
}
|
|
}
|