Hello everyone! I’m working on a 2D game wherein there is involvement of slopes. Now, within the case of jumping, the player jumps, reaches a height but falls back slowly like a feather.
Similar behavior is seen in the case of player movement as well, when player moves, and at the time of falling, it falls down slowly.
Note:- I’m using apply root motions in the animator. I hope that doesn’t cause this behaviour.
Here’s is the code i’m using for jumping:-
private void Update()
{
if (IsGrounded == true)
{
extraJumps = 1;
}
if (Input.GetKeyDown(KeyCode.Space) && extraJumps > 0 && PlayerCanMove == true)
{
if (CheckGrounded() == true)
{
Catanim.SetBool("isJumping", true);
IsJumping = true;
RBody.AddForce(Vector2.up * JumpForce * 1000f);
extraJumps = extraJumps - 1;
}
else
{
IsJumping = false;
Catanim.SetBool("isJumping", false);
}
}
}
}
The typical cause for “slow falling” issues I’ve seen involve setting a Rigidbody's velocity directly for movement (e.g. in FixedUpdate()) and negating vertical speed in the process. Between frames, when gravity and drag are processed for Rigidbodies with it enabled, they receive a small acceleration downward again, which is then negated again on the next frame, and so on. This is also made similarly likely by your massive multiplication toward your jumping force, which I’ll address later.
Second, addressed essentially toward @logicandchaos comment, increasing mass specifically and explicitly WILL NOT increase the rate that an object falls from gravity. Modifying the gravity vector will, certainly. But mass is irrelevant to gravity and, in the case of simple game physics, drag.
Finally, for the purposes of jumping, I would recommend reducing your jumping force by a lot (default 50x) and using a more appropriate ForceMode(2D) to accommodate jumping, instead:
float jumpForce = 5.0f; // Simple estimate jump force
// ...
// Multiply by mass to negate it, since it's a character jumping,
// and not an inanimate object being pushed, dependent on its weight.
RBody.AddForce(Vector2.up * jumpForce * RBody.mass, ForceMode2D.Impulse);
For additional reading, I noted a simple function/formula for calculating a specific jump height in an answer to a previous question.
@hallmk7 Its most likely the issue is related to using Update for physics calls (AddForce), instead of FixedUpdate where they’re supposed to occur. Try changing the AddForce to FixedUpdate and let us know how it goes.