Reputation: 1181
I'm learning how to use Unity3D and I'm on an animation problem. I have a GameObject, which is my character and I want him to jump. In a first place I used an input from the keyboard, the Space Bar.
jumpForce is public float variable and its values is 700
if (grounded && Input.GetKeyDown(KeyCode.Space)) {
anim.SetBool("Ground", false);
rigidbody2D.AddForce(new Vector2(0, jumpForce));
}
Then I tried to get the input from the XBox360 Controller Pad and everything's fine. But with the same code (just changed the GetKeyDown to GetButton for controller) the character jumps higher. With the controller, the max velocity reached is 53 (I get the value of rigidbody2D.velocity.y), but with the keyboard the value is at leat 13. If I change the value of jumpForce nothing happens, the values remain the same, but if I set it to 0 the character won't jump.
I would like to know which is the utility of the variable jumpForce and of the function AddForce, if it doesn't change the value of the speed. Thank you
Upvotes: 0
Views: 1229
Reputation: 11933
If you are experimenting different behaviours when dealing with physics then you are probably using Update
instead of FixedUpdate
, that is, you're applying forces at a different rate.
According to the documentation:
FixedUpdate should be used instead of Update when dealing with Rigidbody. For example when adding a force to a rigidbody, you have to apply the force every fixed frame inside FixedUpdate instead of every frame inside Update.
Upvotes: 1
Reputation: 364
Try:
rigidbody2D.AddForce(Vector2.up * jumpForce);
Just a note, you can probably get more comprehensive answers from Unity's community site
Upvotes: 3