Muhammed Enes Ekinci
Muhammed Enes Ekinci

Reputation: 37

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

Upvotes: 0

Views: 351

Answers (1)

Ruzihm
Ruzihm

Reputation: 20269

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

Upvotes: 1

Related Questions