【问题标题】:why my variable wont change to what it should be in unity为什么我的变量不会改变为统一的值
【发布时间】:2022-07-04 21:21:50
【问题描述】:

所以当我运行“Running”代码时,我的 moveSpeed 变量不会变为 4。当我添加“Crouching”代码时会发生这种情况。当我删除或评论蹲下的代码时,我的运行代码运行良好

这是我的代码

    //Running
    if (Input.GetKey(KeyCode.LeftShift))
    {
        animator.SetBool("isRunning", true);
        moveSpeed = 4;
    }
    else
    {
        animator.SetBool("isRunning", false);
        moveSpeed = 2;
    }

    //Crouching
    if (Input.GetKey(KeyCode.C))
    {
        animator.SetBool("isCrouching", true);
        moveSpeed = 1;
    }
    else
    {
        animator.SetBool("isCrouching", false);
        moveSpeed = 2;
    }

我是游戏开发的新手,我会很感激任何建议

【问题讨论】:

  • 这在你的更新功能中吗?
  • 我建议你把这个添加到FixedUpdate()函数中
  • 是的,我把它放在更新方法中,为什么?
  • 最好在fixedupdate中做运动或物理逻辑,

标签: c# unity3d


【解决方案1】:

我假设你没有按 C 并且 moveSpeed 被“卡”在 2 上。

    if (Input.GetKey(KeyCode.LeftShift))
    {
        animator.SetBool("isRunning", true);
        moveSpeed = 4;
    }
    else if (Input.GetKey(KeyCode.C))
    {
        animator.SetBool("isCrouching", true);
        moveSpeed = 1;
    }
    else
    {
        animator.SetBool("isCrouching", false);
        animator.SetBool("isRunning", false);
        moveSpeed = 2;
    }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-06-20
    • 1970-01-01
    • 1970-01-01
    • 2014-11-15
    • 2016-10-29
    • 1970-01-01
    • 1970-01-01
    • 2011-11-25
    相关资源
    最近更新 更多