- Home /
2D walls affect my jump height
I have created a 2D runner style game. When the player jumps up against a wall there jump height can be increased.
This is how I make the player jump: rigidbody2D.AddForce(new Vector2(0f, jumpForce));
Horizontal speed is constantly added to the character as follows: rigidbody2D.velocity = new Vector2(speed, rigidbody2D.velocity.y);
The player is made up of a box collider and a circle collider both of which have no friction.
The walls have a side box collider that is frictionless wherever the player can jump against other wise they will get stuck on the wall.
I have read through a few answers that claim adding a Rigidbody to the wall and enabling its Is Kinematic property will cancel out these affects. http://answers.unity3d.com/questions/420946/why-is-my-jump-height-affected-by-walls.html
I have tried adding a Rigidbody2D to a wall and enabling its Is Kinematic property but the affect seems to be the same.
My current solution is adding small amounts of friction depending on the wall height to reduce the players jump height. This does not produce constant results and slows the player down too much which I do not want.
Answer by PouletFrit · Jun 04, 2014 at 11:13 AM
Since your player already have a rigidbody and your wall a collider, OnCollisionEnter should trigger on your wall, so you just have to add a script on it that will reduce the velocity of your player on the y axis.
You can either directly cut off the velocity or scale it down... depends on your needs. The gravity should take care of the rest.
public void OnCollisionEnter2D(Collision2D coll) {
// Add the tag player to your player gameObject
if (coll.gameObject.tag == "Player") {
vel = coll.rigidbody.velocity;
// Now reduce the velocity on y axis
// You may just directly cut if off
vel.y = Mathf.Max(0, vel.y - reduceJumpingForce);
// or, scale it down
vel.y *= 0.5f;
coll.rigidbody.velocity = vel;
}
}