watanuki50 2025-03-08 12:52 采纳率: 0%
浏览 3

提问,parameters下面的bool参数不响应怎么办

img

img

scripts是这样的
public class Player : MonoBehaviour
{
public Rigidbody2D rb;
private Animator anim;

[SerializeField] private float jumpForce;
[SerializeField] private float moveSpeed;

[SerializeField] private bool IsMoving;
private float xInput;
// Start is called before the first frame updat
void Start()
{
    rb = GetComponent<Rigidbody2D>();
    anim = GetComponentInChildren<Animator>();
}

// Update is called once per frame
void Update()
{
    xInput = Input.GetAxisRaw("Horizontal");
    

    rb.velocity = new Vector2(xInput*moveSpeed, rb.velocity.y);
    if (Input.GetKeyDown(KeyCode.Space))
    {
        rb.velocity = new Vector2(rb.velocity.x, jumpForce);
    }



    IsMoving = rb.velocity.x != 0;
    anim.SetBool("IsMoving", IsMoving);

}

}public class Player : MonoBehaviour
{
public Rigidbody2D rb;
private Animator anim;

[SerializeField] private float jumpForce;
[SerializeField] private float moveSpeed;

[SerializeField] private bool IsMoving;
private float xInput;
// Start is called before the first frame updat
void Start()
{
    rb = GetComponent<Rigidbody2D>();
    anim = GetComponentInChildren<Animator>();
}

// Update is called once per frame
void Update()
{
    xInput = Input.GetAxisRaw("Horizontal");
    

    rb.velocity = new Vector2(xInput*moveSpeed, rb.velocity.y);
    if (Input.GetKeyDown(KeyCode.Space))
    {
        rb.velocity = new Vector2(rb.velocity.x, jumpForce);
    }



    IsMoving = rb.velocity.x != 0;
    anim.SetBool("IsMoving", IsMoving);

}

}

  • 写回答

5条回答 默认 最新

  • 阿里嘎多学长 2025-03-08 13:10
    关注

    阿里嘎多学长整理AIGC生成,因移动端显示问题导致当前答案未能完全显示,请使用PC端查看更加详细的解答过程

    解答

    你遇到了一个 Unity 中的 bool 参数不响应的问题,这可能是因为你在 Unity 中使用了 bool 类型的参数,但是 Unity 中的 bool 类型参数默认是 false,除非你手动将其设置为 true

    在你的代码中,你使用了 public Rigidbody2D rb;private Animator anim,这两个变量都没有被赋值,这可能是导致 bool 参数不响应的原因。

    你可以尝试在 Start() 方法中将 rbanim 变量赋值为 true,例如:

    public class Player : MonoBehaviour
    {
        public Rigidbody2D rb;
        private Animator anim;
    
        private void Start()
        {
            rb = GetComponent<Rigidbody2D>();
            anim = GetComponent<Animator>();
        }
    }
    

    这样,rbanim 变量将被赋值为 true,这可能会解决 bool 参数不响应的问题。

    如果你仍然遇到问题,可以提供更多的代码和详细信息,以便更好地帮助你解决问题。

    评论

报告相同问题?

问题事件

  • 创建了问题 3月8日