- Home /
how to control gravity for addforce ?
So i just wanna know how i could go about controlling the gravity for the rigidbodys addforce function ? like when i jump the player reaches the end of his jump and like floats for maybe 0.2 seconds, then drops down again, it feels very floaty and unrealistic (not that the game is supposed to be realistic, but u get me), and he also shoots up in the air whenever i double jump.
Ive tried setting the gravity in the physics2D project settings, from the default -9.81 to like -15 - -19. But it feels like its too much. The box collider on my character is 1 unit tall, and the objects in the scene are two units tall, if that helps. my question is: how do i make the jump so that it feels more realistic, so that the character falls directly to the ground after reaching the jump limit, and doesnt float for 0.2 seconds, and also doesnt shoot straigt up in the air when i doublejump, but jumps with same force as the first jump.
My movement code is:
void FixedUpdate()
{
var absVelY = Mathf.Abs (rigidbody2D.velocity.y);
rigidbody2D.velocity = new Vector2 (move * maxSpeed, rigidbody2D.velocity.y);
if (controller.moving.x != 0)
{
transform.localScale = new Vector3 (controller.moving.x > 0? 1 : -1, 1, 1);
animator.SetInteger ("AnimState", 1);
} else
{
animator.SetInteger ("AnimState", 0);
}
if (absVelY < 0.02f)
{
grounded = true;
}
else
{
grounded = false;
}
if (grounded)
{
jumpCount = 0;
}
if (jumpCount < maxJump)
{
if (Input.GetKeyDown ("space"))
{
rigidbody2D.AddForce (new Vector2 (0, jumpForce));
jumpCount++;
}
}
}
For the double jump problem refer to this live training tutorial: http://unity3d.com/learn/tutorials/modules/beginner/live-training-archive/infinite-runner
If I remember correctly you need to reset the velocity to zero upon adding the second jump.. but I might be off.
ah yea thats true, ill try and fiddle around with that, thanks, ill return with results
Answer by senc01a · Aug 19, 2014 at 10:25 AM
Jump more realistic
You can do this by increasing the amount of gravity (like you did with -19). Try -40 for instance. Keep in mind that your jumpforce needs to be significantly higher as well. Play with higher values of the jumpforce and gravity and you will see how you get a much snappier jump.
Double jump same as the first jump This is somewhat more tricky. You are using forces to move the rigidbody. Because of this, you need to calculate the force that it would take the counter the current vertical speed, plus the force to get the player to jump same as it did on the first jump. This is rather difficult, so a much easier way is to simply set velocities instead of adding forces.
So basically in line 31, instead of applying a force rigidbody2D.AddForce (new Vector2 (0, jumpForce));
, set the velocity similar to what you do in line 5
rigidbody2D.velocity = new Vector2(rigidbody2D.velocity,x, 5.0);
Hey thanks for your reply. Hmm it kinda works this way, it jumps with the same force each time now it seems when using velocity, so ur answer helped, but still it's kinda meh.. it's more of a value tweaking question i think, than a direct approach.. i'll vote your answer up :)