Unity敌人脚本异常:无法追踪玩家但仍可造成伤害问题求助
问题现象
敌人脚本最初运行正常,现在靠近敌人时它不会追踪玩家,但接触敌人时仍能正常造成伤害。
敌人脚本代码
using System.Collections; using System.Collections.Generic; using UnityEngine; public class Enemy : Mover { // Experience public int xpValue = 1; // Logic public float triggerLength = 1; public float chaseLength = 5; private bool chasing; private bool collidingWithPlayer; private Transform playerTransform; private Vector3 startingPosition; // Hitbox public ContactFilter2D filter; private BoxCollider2D hitbox; private Collider2D[] hits = new Collider2D[10]; protected override void Start() { base.Start(); playerTransform = GameManager.instance.player.transform; startingPosition = transform.position; hitbox = transform.GetChild(0).GetComponent<BoxCollider2D>(); } private void FixedUpdate() { // Is the player in range? if (Vector3.Distance(playerTransform.position, startingPosition) < chaseLength) { if (Vector3.Distance(playerTransform.position, startingPosition) < triggerLength) chasing = true; if (chasing) { if (!collidingWithPlayer) { UpdateMotor((playerTransform.position - transform.position).normalized); } } else { UpdateMotor(startingPosition - transform.position); } } else { UpdateMotor(startingPosition - transform.position); chasing = false; } // Check for overlaps collidingWithPlayer = false; boxCollider.OverlapCollider(filter, hits); for (int i = 0; i < hits.Length; i++) { if (hits[i] == null) continue; if (hits[i].tag == "Fighter" && hits[i].name == "Player") { collidingWithPlayer = true; } // The array is not cleaned up, so we do it ourself hits[i] = null; } } protected override void Death() { Destroy(gameObject); GameManager.instance.GrantXp(xpValue); GameManager.instance.ShowText("+" + xpValue + " xp", 30, Color.magenta, transform.position, Vector3.up * 40, 1.0f); } }
错误日志说明
以下错误属于Unity编辑器的可视化图(如状态机、Shader Graph等)异常,与运行时的敌人追踪逻辑无关,不会影响游戏运行:
错误: NullReferenceException: 对象引用未设置到对象的实例
UnityEditor.Graphs.Edge.WakeUp () (at <06397f6908034403aa752a7eb86035b0>:0)
UnityEditor.Graphs.Graph.DoWakeUpEdges (System.Collections.Generic.List1[T] inEdges, System.Collections.Generic.List1[T] ok, System.Collections.Generic.List`1[T] error, System.Boolean inEdgesUsedToBeValid) (at <06397f6908034403aa752a7eb86035b0>:0)
UnityEditor.Graphs.Graph.WakeUpEdges (System.Boolean clearSlotEdges) (at <06397f6908034403aa752a7eb86035b0>:0)
UnityEditor.Graphs.Graph.WakeUp (System.Boolean force) (at <06397f6908034403aa752a7eb86035b0>:0)
UnityEditor.Graphs.Graph.WakeUp () (at <06397f6908034403aa752a7eb86035b0>:0)
UnityEditor.Graphs.Graph.OnEnable () (at <06397f6908034403aa752a7eb86035b0>:0)
追踪失效的原因与修复
核心问题
脚本中判断玩家是否在追踪范围内时,使用的是敌人初始位置与玩家位置的距离,而非敌人当前位置与玩家的距离。一旦敌人离开初始位置,即使玩家就在敌人身边,只要离初始位置超出chaseLength范围,敌人就不会触发追踪。
修复后的FixedUpdate代码
private void FixedUpdate() { // 计算玩家与敌人当前位置的距离 float distanceToPlayer = Vector3.Distance(playerTransform.position, transform.position); // Is the player in range? if (distanceToPlayer < chaseLength) { if (distanceToPlayer < triggerLength) chasing = true; if (chasing) { if (!collidingWithPlayer) { UpdateMotor((playerTransform.position - transform.position).normalized); } } else { UpdateMotor(startingPosition - transform.position); } } else { UpdateMotor(startingPosition - transform.position); chasing = false; } // Check for overlaps collidingWithPlayer = false; boxCollider.OverlapCollider(filter, hits); for (int i = 0; i < hits.Length; i++) { if (hits[i] == null) continue; if (hits[i].tag == "Fighter" && hits[i].name == "Player") { collidingWithPlayer = true; } // The array is not cleaned up, so we do it ourself hits[i] = null; } }
额外检查项
- 确认
GameManager.instance.player不为空,确保玩家对象在场景中且正确赋值 - 检查
triggerLength和chaseLength的数值设置,确保triggerLength < chaseLength
内容的提问来源于stack exchange,提问作者Niks Rodžers

