Unity 2D角色跳跃太多

Unity 2D Character Too Much Jumping

首先抱歉我的英语不好。我的问题很清楚;

我的性格有时会跳得很高

正常; gif; normal jump

如果角色跳到对撞角,有时会发生这种情况; gif; anormal high jump

为什么会这样?我该如何解决这个问题?

这是我的代码;

    private void FixedUpdate()
{
    jumpButton = GameObject.Find("Jump").GetComponent<Button>();
    jumpButton.onClick.AddListener(Jump);

    groundCheck = GameObject.Find("GroundCheck").GetComponent<Transform>();
    isGrounded = Physics2D.OverlapCircle(groundCheck.position, checkRadius, whatIsGround);

    MoveInput = SimpleInput.GetAxisRaw("Horizontal");
    rb.velocity = new Vector2(MoveInput * speed, rb.velocity.y);

    if (isGrounded && jump)
    {
        rb.AddForce(Vector2.up * jumpForce, ForceMode2D.Impulse);
        jump = false;
    }
}

public void Jump()
{
    jump = true;
}

按照你的方式,你每次跳跃都可以加速向上运动。

为了使每次跳跃产生相同的速度,只需将y速度设置为某个值即可。我们可以使用 jumpForce/rb.mass 来获得与使用 AddForceForceMode2D.Impulse 产生的相同的值。

private void FixedUpdate()
{
    jumpButton = GameObject.Find("Jump").GetComponent<Button>();
    jumpButton.onClick.AddListener(Jump);

    groundCheck = GameObject.Find("GroundCheck").GetComponent<Transform>();
    isGrounded = Physics2D.OverlapCircle(groundCheck.position, checkRadius, whatIsGround);

    MoveInput = SimpleInput.GetAxisRaw("Horizontal");
    rb.velocity = new Vector2(MoveInput * speed, rb.velocity.y);

    if (isGrounded && jump)
    {
        rb.velocity = new Vector2(rb.velocity.x, jumpForce/rb.mass);
        jump = false;
    }
}