先看代码:
public bool isGround, isJump;//地面检测,跳跃检测
private bool jumpPressed;//跳跃按键检测
public int jumpCount,MaxjumpCount;//当前可跳跃次数,最大跳跃数
public float jumpGravity;
if (Input.GetButtonDown("Jump") && jumpCount > 0)
{
jumpPressed = true;
}
if (isGround)
{
jumpCount = MaxjumpCount;
rb.gravityScale = 1;
isJump = false;
}
if (jumpPressed && isGround)
{
isJump = true;
rb.velocity = new Vector2(rb.velocity.x,jumpForce);
jumpCount--;
jumpPressed = false;
}
else if(jumpPressed && isJump)
{
rb.velocity = new Vector2(rb.velocity.x, jumpForce*0.8f);
jumpCount--;
jumpPressed = false;
}
看看逻辑,跳跃键按下,jumpPressed = true,落地后isGround = true,因此落地立即触发跳跃
解决方法:
isGround = Physics2D.OverlapCircle(groundCheck.position, 0.1f, ground);
if (Input.GetButtonDown("Jump") && jumpCount > 0)
{
if(isGround||isJump) jumpPressed = true;
}
在地面时或进行二段跳(多段跳)时,跳跃键按下才是合法的。