简体   繁体   中英

Unity 2D Character Too Much Jumping

Firstly sorry for my bad english. My problem so clear;

My character sometimes jump to very high.

Normally; gif; normal jump

sometimes this happening if character jump to collider corner; gif; anormal high jump

Why this happening? How I can fixed this problem?

that's my codes;

    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;
}

The way you have it, you are able to accelerate your upward movement everytime you jump.

In order to make the jump produce the same velocity each time, just set the y velocity to some value. We can use jumpForce/rb.mass to get the same value that using AddForce with ForceMode2D.Impulse produces.

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;
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM