Hello everyone, I should include in into my hero of my 2d game the jump function. It has an rigidbody2D with gravity = 30. Thought I’d put this code in FixedUpdate script attached to the character:
Into Update method I control if the user push the Space bar for jump, if it’ true a set bool jump to true and then
if (jump) rigidbody2d.Addforce (transform.up * jumpPower);
//And then I control if the hero Y position is over the camera size
if (transform.position.y >= Camera.main.getComponent <Camera>.orthograficSize)
jump = false;
/ / At this point the gravity = 30 brings it down
This method is good for you ? Is there any other better method? Why I must set gravity to 30 if the hero’s mass is 1 ? It should not be hero mass = 1 and gravity = 1?
Where did you read that the gravity scale should be 30? You shouldn’t need to touch the gravity scale in most cases, except for very specific jumping styles.
Don’t constantly AddForce for a jump, that looks unrealistic, a jump, just like in real life, is 1 instance of force from the ground, and the rest is just the person/player’s velocity allowing them to move upwards, until gravity finally brings them back down.
You can simply do:
if(jump)
{
rigidbody2D.velocity = new Vector2(rigidbody2D.velocity.x,jumpPower);
jump = false;
}
Just leave your gravity as 1-9 or around there, you shouldn’t need gravity 30, unless perhaps you’re using a very massive scene scale, which you shouldn’t be.