跳动画应该只播放一次

时间:2020-03-01 22:59:10

标签: c# unity3d

public void JumpingStuff()
{
    if (_grounded == true)
    {
        playerAS.clip = playerjump2;
        playerAS.Play();
        extraJumps = extraJumpsValue;
        m_Jump = true;

        //here allow to end the animation if some time was passed
        if (!hasJumped && m_Animator.GetBool("Jumping"))
        {
            m_Animator.SetBool("Jumping", false);
        }
    }
    //(Input.GetKeyDown(KeyCode.Space) && extraJumps > 0)
    // PC
    if (extraJumps > 0)
    {
        rb.velocity = Vector2.up * jumpForce;
        extraJumps--;

        if (m_Jump == true)
            m_Animator.SetBool("Jumping", true);
        playerAS.clip = playerjump;
        playerAS.Play();
        //Debug.Log("playing jumping animation");

        //here your player has began the jump
        if (!hasJumped)
        {
            hasJumped = true;
            StartCoroutine("WaitBitAfterJump");

            //Play sound when he jump
            //playerAS.PlayOneShot(playerjump);
        }
    }
    //(Input.GetKeyDown(KeyCode.Space) && extraJumps == 0 && _grounded == true)
    //PC
    else if (extraJumps == 0 && _grounded == true)
    {
        rb.velocity = Vector2.up * jumpForce;
        rb.velocity = new Vector2(rb.velocity.x, jumpForce);
        m_Animator.SetBool("Jumping", false);
        Debug.Log("I am Jumping");
    }
}

请帮助我修复代码(我知道这是我的第一次,很麻烦)

我正在从PC> mobile更改我的代码,但出现以下错误:

仅跳一次应该是两次 并仅第一次播放动画

1 个答案:

答案 0 :(得分:0)

您可能在行中缺少“ else”

if (extraJumps > 0)

因此,当您输入JumpingStuff并且满足第一个条件时

if (_grounded == true)

它设置您的extraJumps值,并且第二个条件也得到满足,因此两个跳转都在同一时间执行。

剥离掉不必要的代码后,它看起来像这样:

if( grounded == true ){
    PlayJumpAnimation();
    extraJumps = 1;
}

if(extraJumps > 0){
    PlayJumpAnimation();
    extraJumps--;
}

因此,基本上,当接地== true时,extraJumps设置为1且extraJumps> 0也是true,因此PlayJumpAnimation();和同时执行而没有其他操作,它应该看起来像这样:

if( grounded == true ){
    PlayJumpAnimation();
    extraJumps = 1;
}
else if(extraJumps > 0){
    PlayJumpAnimation();
    extraJumps--;
}