1
\$\begingroup\$

I'm having troubles implementing a satisfying wall jump in Unity. I managed to detect collision with a wall, but when I press the jump button, the result is alway weird or very weak. I think this have to do with the 2D physic in Unity, but I have no idead how to handle it correctly.

Here's my code with the concerned parts:

public void Move(float move, bool crouch, bool jump)
{
    // If the input is moving the player right and the player is facing left...
    if (move > 0 && !facingRight)
        // ... flip the player.
        Flip();
    // Otherwise if the input is moving the player left and the player is facing right...
    else if (move < 0 && facingRight)
        // ... flip the player.
        Flip();

    if (jump)
    {
        if (!grounded && walled)
        {
            StartCoroutine(WallJumpRoutine());
        }
    }
}

void Flip()
{
    // Switch the way the player is labelled as facing.
    facingRight = !facingRight;

    // Multiply the player's x local scale by -1.
    Vector3 theScale = transform.localScale;
    theScale.x *= -1;
    transform.localScale = theScale;
}

IEnumerator WallJumpRoutine()
{
    Vector2 wallJumpVector = new Vector2(jumpForce*10, 20f);

    GetComponent<Rigidbody2D>().AddForce(wallJumpVector, ForceMode2D.Impulse);
    yield return null;
}

To be more precise, the result I'm getting is either my player being moved a tiny bit (despite using the same force as my regular jump, which is working fine) or my player being moved a good distance but at very high speed (this is when I'm using ForceMode2D.Impulse).

Any help?

\$\endgroup\$
  • \$\begingroup\$ Making the player a Rigidbody is rarely a good idea. You usually want the player to move in a way which feels correct while not necessarily being correct from a purely physical standpoint. \$\endgroup\$ – Philipp Sep 30 '16 at 12:41
0
\$\begingroup\$

The velocity gained from AddForce in any particular direction is affected my a multitude of factors, including the current velocity of the rigidbody.

Two things you can try:

1) Setting velocity to zero before applying the force

GetComponent<Rigidbody2D>().velocity = Vector3.zero;
GetComponent<Rigidbody2D>().AddForce(wallJumpVector);

OR

2) Ditch addforce all together and control the velocity directly.

GetComponent<Rigidbody2D>().velocity = wallJumpVector;

Hope that helps.

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy