Unity C#方法忽略类作用域isDodging变量值变更致闪避逻辑失效
问题核心
布尔类型变量isDodging在Dodge()方法中被设置为true时,本该触发FixedUpdate()中调用的Movement()方法内的对应分支,但该代码块始终被跳过。
涉及核心代码
MovementController类实现
using UnityEngine; public class MovementController { /* 组件引用 */ public Rigidbody2D Rigidbody { private get; set; } /* 状态变量 */ private bool isDodging = false; private Vector2 dodgeDirection = Vector2.right; private float dodgeDuration = 1f; private float dodgeSpeed = 20f; private float timer = 0f; /* 核心方法 */ // 物理移动逻辑,在FixedUpdate中调用 public void Movement(Vector2 currentPosition, Vector2 velocity) { Debug.Log("In movement: " + isDodging); if (isDodging) { Debug.Log("Dodge 3"); Move(currentPosition, dodgeDirection * dodgeSpeed); timer += Time.fixedDeltaTime; if (timer >= dodgeDuration) { Debug.Log("Stopped dodging " + Time.fixedTime); isDodging = false; } } else { Move(currentPosition, velocity); } } private void Move(Vector2 currentPosition, Vector2 velocity) { if (Rigidbody == null) { Debug.LogWarning("No rigidbody to move!"); return; } Rigidbody.MovePosition(currentPosition + (velocity * Time.fixedDeltaTime)); } // 闪避触发方法 public void Dodge(Vector2 direction, float maxSpeed, float speedMultiplier = 2f, float duration = 1f) { if (direction == Vector2.zero) { return; } Debug.Log("Dodge 1 " + isDodging); dodgeDirection = direction; dodgeDuration = duration; dodgeSpeed = maxSpeed * speedMultiplier; isDodging = true; Debug.Log("Dodge 2" + isDodging + Time.fixedTime); timer = 0f; } }
调用层CreatureMovement类实现
两个方法均在独立MonoBehaviour类CreatureMovement中被调用,其中Dodge()由玩家输入触发,代码如下:
public class CreatureMovement : MonoBehaviour { [Header("Movement")] [SerializeField] protected Vector2Reference moveDirection; [SerializeField] protected FloatReference maxSpeed; [Header("Dodge")] [SerializeField] private FloatReference dodgeDuration; [SerializeField] private FloatReference dodgeSpeedMultiplier; [Header("References")] [SerializeField] private new Rigidbody2D rigidbody; private readonly MovementController movement = new MovementController(); public float MaxSpeed { get => maxSpeed; } private float speed; private float Speed { get => speed; set => speed = Mathf.Clamp(value, 0, maxSpeed); } public virtual Vector2 Velocity { get => moveDirection.Value * Speed; set { moveDirection.SetValue(value.normalized); Speed = value.magnitude; } } private void Start() => movement.Rigidbody = rigidbody; private void FixedUpdate() => movement.Movement(transform.position, Velocity); public void Dodge() => movement.Dodge(moveDirection, maxSpeed, dodgeSpeedMultiplier, dodgeDuration); }
实际异常表现
Movement()方法内打印的isDodging始终为false,对应闪避分支从未执行Dodge()方法内紧邻赋值操作后的日志,可正确显示isDodging为true- 首次调用
Dodge()时打印的isDodging为false,后续每次调用却显示为true,仿佛isDodging的变更无法被Movement()方法感知
已完成排查点
- 普通移动功能完全符合预期
Move()方法未涉及isDodging变量,已排除该方法导致异常的可能
问题根因与修复方案
异常根因来自参数类型不匹配导致的调用错位:
CreatureMovement中定义的moveDirection、maxSpeed、dodgeSpeedMultiplier、dodgeDuration均为自定义Reference类型(用于包装常量/ScriptableObject变量的序列化包装类),不属于原生的Vector2/float类型- 在
Dodge()方法中直接将Reference实例传入MovementController.Dodge()方法时,由于没有实现对应的隐式类型转换,代码实际没有调用预期的movement实例的方法,而是触发了其他同签名的重载方法(例如自动匹配到的扩展方法、其他模块的同名实现),修改的是无关实例的isDodging值 - 这也完美匹配日志表现:被错误调用的实例首次初始化时
isDodging为false,赋值后变为true,因此后续调用该实例时会打印true;而FixedUpdate中实际使用的movement实例的isDodging从未被修改,因此始终打印false,永远不会进入闪避分支。
修复方法:调用movement.Dodge()时,显式传递Reference实例内部存储的实际值,而非Reference对象本身:
public void Dodge() => movement.Dodge( moveDirection.Value, maxSpeed.Value, dodgeSpeedMultiplier.Value, dodgeDuration.Value );
内容的提问来源于stack exchange,提问作者Francisco Cunha
相关产品推荐
相关产品推荐

