首页 > 解决方案 > 跳跃动画在第一次跳跃中结束太快

问题描述

    void FixedUpdate()
    {
        moveInput = Input.GetAxis("Horizontal");
        rb.velocity = new Vector2(moveInput * speed, rb.velocity.y);
        anim.SetBool("isWalking", true);
        if (facingRight == false && moveInput>0)
        {
            flip();
        }
        else if (facingRight == true && moveInput < 0)
        {
            flip();
        }
        else if (moveInput == 0)
        {
            anim.SetBool("isWalking", false);
        }

        isGrounded = Physics2D.OverlapCircle(groundCheck.position, checkRadius, whatIsGround);

    }

    void Update()
    {
        if (isGrounded == true)
        {
            Debug.Log("is grounded");
            extraJumps = extraJumpValue;
            anim.SetBool("isJumping", false);
        }
        if (Input.GetKeyDown(KeyCode.Space) && extraJumps + 1.0f > 0)
        {
            jump();
            extraJumps--;
        }
    }

    void flip()
    {
        facingRight = !facingRight;
        Vector3 Scaler = transform.localScale;
        Scaler.x *= -1;
        transform.localScale = Scaler;
    }
    void jump()
    {
        rb.velocity = Vector2.up * jumpForce;
        anim.SetBool("isJumping", true);
    }
}

我刚开始学习统一,是从 YouTube 上的教程开始的。但是我不明白为什么我的跳跃动画在我第一次跳跃时就结束了。在额外的跳跃中它工作正常。如果您知道如何解决此问题,请帮助我。

标签: c#visual-studiounity3d

解决方案


在您的更新循环中,您调用 Jump 设置

    anim.SetBool("isJumping", true);

在您的固定更新循环中,您有

    anim.SetBool("isWalking", true);

因此,在下一个“FixedUpdate”调用 Jump 之后,您立即设置 iswalking 的动画布尔值,除非 moveInput 为 0。我看不到您的所有代码或 moveInput 发生的情况,但我可以告诉您这是您的问题所在. 您需要更改此代码的动画:

    anim.SetBool("isWalking", true);
    if (facingRight == false && moveInput>0)
    {
        flip();
    }
    else if (facingRight == true && moveInput < 0)
    {
        flip();
    }
    else if (moveInput == 0)
    {
        anim.SetBool("isWalking", false);
    }

推荐阅读