微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

方法忽略类范围变量值变化

如何解决方法忽略类范围变量值变化

问题在于布尔值 isDodging。它在 Dodge() 方法中设置为 true。这应该会触发 Movement() 方法(在 FixedUpdate()调用)中的 if 语句,但该块总是被跳过。我附上了所有班级的代码,因为这里一定有我遗漏的东西:

using UnityEngine;

public class MovementController
{
    /* COMPONENTS */
    public Rigidbody2D Rigidbody { private get; set; }

    /* VARIABLES */
    private bool isDodging = false;
    private Vector2 dodgeDirection = Vector2.right;
    private float dodgeDuration = 1f;
    private float dodgeSpeed = 20f;
    private float timer = 0f;

    /* METHODS */
    
    // Called in fixed update (since it's dealing with physics)
    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));
    }

    // Must be called while Movement is being called
    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;
    }
}

问题是,“在运动中:”日志总是将 isDodging 显示false,并且它下面的 if 块永远不会运行。同时,“Dodge 2”将显示 true(因为 isDodging 在其正上方发生了变化)。最奇怪的是:“Dodge 1”在第一次调用 false显示 Dodge(),但之后每次调用 true - 好像 isDodging 在类范围,而 Movement() 由于某种原因无法识别。

这两个函数都在单独的 MonoBehavIoUr 中调用

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,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);
}

从玩家输入调用 Dodge() 的地方。

除了闪避之外,运动完全符合预期。问题可能不在 Move() 方法中,因为它没有 isDodging

我完全不知道为什么会发生这种情况,代码对我来说似乎很简单,但它就是行不通。请帮忙解决这个问题。

解决方法

您在预制件上调用 Dodge,而不是在运行 CreatureMovement.FixedUpdate 的该预制件的场景实例上调用。

我相信您可以通过将此代码放入 Dodge 来验证这一点(来源:Max-Pixel):

if (gameObject.scene.name == null) Debug.Log("It's a prefab!");

您需要更改输入处理以在场景中的实例而不是预制件上调用 Dodge

您可以通过将场景中的实例拖动到按钮 onclick 事件中,然后选择 Dodge 来实现。或者,如果您动态生成对象,则可以在 Start 中找到对该按钮的引用,并将 Dodge 添加到其 onClick 侦听器:

private void Start()
{
    movement.Rigidbody = rigidbody;  
    // something along the lines of this...
    Button buttonRef = GameObject.Find("ButtonName").GetComponent<Button>();

    buttonRef.onClick.AddListener(Dodge);
}

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐


Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其他元素将获得点击?
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。)
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbcDriver发生异常。为什么?
这是用Java进行XML解析的最佳库。
Java的PriorityQueue的内置迭代器不会以任何特定顺序遍历数据结构。为什么?
如何在Java中聆听按键时移动图像。
Java“Program to an interface”。这是什么意思?