W
W
WasTabon2022-03-10 01:30:37
Unity
WasTabon, 2022-03-10 01:30:37

Why is the character always jumping to different heights in a platformer?

if (hit.collider != null)
        {
            Debug.Log(hit.collider.gameObject.name);
            if (isGrounded() && hit.collider.gameObject.name != "Player")
            {
                rigidbody2D.AddForce(Vector2.up * jumpForce, ForceMode2D.Impulse);
            }
        }

62292a475836e901796887.png
62292a5e2cfbf093280662.png
Can jump high or low
jumpForce does not change
IsGrounded() code
private bool isGrounded()
    {
        float extraHeight = 0.1f;
        RaycastHit2D raycastHitGround = Physics2D.Raycast(boxCollider2D.bounds.center, Vector2.down, boxCollider2D.bounds.extents.y + extraHeight);
        Color rayColor;
        if (raycastHitGround.collider != null)
        {
            rayColor = Color.green;
        }
        else
        {
            rayColor = Color.red;
        }
        Debug.DrawRay(boxCollider2D.bounds.center, Vector2.down * (boxCollider2D.bounds.extents.y + extraHeight));
        return raycastHitGround.collider != null;
    }

Answer the question

In order to leave comments, you need to log in

1 answer(s)
F
freeExec, 2022-03-10
@WasTabon

So you apply force several times.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question