- Home /
Question by
LeoM1234 · Sep 03, 2020 at 11:47 AM ·
2drigidbody2djumprigidbody.addforce
My player jumps higher when the ground is aslant?
Hi guys, I am working on my first project and have a problem. When my 2D player walks up a little hill and i jump, it jumps way higher than he would on normal floor. Why is it like this? Does it have something to do with the fact that my players rigidbody2D is Dynamic? Here is the script:
if (m_Grounded && jump)
{
// Add a vertical force to the player.
m_Grounded = false;
m_Rigidbody2D.AddForce(new Vector2(0f, m_JumpForce));
}
I would be really happy if you could help me, Leo Motte
Comment
Best Answer
Answer by Acdia · Sep 03, 2020 at 12:25 PM
Rigidbody.AddForce() adds the force to the current velocity. When you are moving up a hill, you have some velocity on the y axis already. One solution would be to set the velocity:
m_Rigidbody2D.velocity = new Vector2(m_Rigidbody2D.velocity.x, m_JumpForce);
Thank you very much! It still feels a little weird when jumping over a hill but it is a lot better now.