Question by
Isheros · Feb 18, 2016 at 09:20 AM ·
c#collision2d-platformerjump2d-physics
2D jump problem when hits wall
Hello, I have a little problem with this, when I do a jump and the characters hits a wall, he jumps more higher than normal... is like he was sliding on the wall.
This is my Jump code, I put this on fixed update:
#region Jump - DobleJump
// Jump
if (Input.GetButton("Jump") && (jumpHeight < maxJumpHeight))
{
// If Max jump is higher than actual jump adds force
rigid2d.AddForce(Vector2.up, ForceMode2D.Impulse);
jumpHeight += 1;
}
// Double Jump
if (Input.GetButtonDown("Jump") && !touchingGround && !doubleJump)
{
doubleJump = true;
rigid2d.velocity = new Vector2(rigid2d.velocity.x, 0);
rigid2d.AddForce(Vector2.up * (jumpForce), ForceMode2D.Impulse);
jumpHeight = maxJumpHeight;
}
if (Input.GetButtonUp("Jump"))
{
jumpHeight = maxJumpHeight;
}
#endregion
And this is my Movement code, I don't know if this interfere with the jump (I think no..)
#region Move - Walk
// Move - Walk
// In the ground
if (touchingGround){
// If horizontal speeds change, move
if (movX * rigid2d.velocity.x < xSpeed)
rigid2d.AddForce(Vector2.right * movX * moveForce);
// If Buttons Not pressed, stop
if (!(Input.GetButton("Right") || Input.GetButton("Left")))
rigid2d.velocity = new Vector2(0, rigid2d.velocity.y);
}
// In the air
else if (!touchingGround){
// In the air, horizontal speed drecrease
if (movX * rigid2d.velocity.x < xSpeed)
rigid2d.AddForce(Vector2.right * movX * (moveForce/4));
}
I tried with 2d physics materials but that don't solve my problem.
Any improvement to code is welcome.
Thanks in advance, and sorry my bad English, and poor code skills C:
Comment