- Home /
2D physics jump issue.
Hey Guys, Im having trouble with this 2D jump. When I jump next a platform it sometimes shoots up really high. From digging around I found that physics should be in fixedupdate but i cant put input in fixedupdate since some input gets lost so im kind of stuck as to how to fix this.
if((grounded /*|| !doubleJump*/) && Input.GetKeyDown(KeyCode.Space))
{
anim.SetBool ("Ground", false);
r.AddForce(new Vector2 (0, jumpForce));
grounded = false;
}
Have you tried putting it in Update? You might find it still works. You could create an object at the top of the scene to "stop" the player from jumping too high however I see that still wouldn't help. Try putting it in update then reply what it does and I will try to help as best I can :)
You can (and should) check user input in Update ins$$anonymous$$d of fixed update since you only apply the force once while jumping. If you did something constantly (apply force every frame, raycast etc.) then use fixed update.
You can also try setting the velocity directly (pseudocode): r.velocity += Vector2.up * jumpAmount
.
Answer by DoTA_KAMIKADzE · Apr 11, 2015 at 06:45 PM
You're completely fine using it in Update, but your code that you have shown failed to represent complete issue, so for now I can just point you in the right direction.
If your jumpForce variable is constant value (if not then well that's why...) then the only possible problem from your code is the problem with grounded variable, more specifically - you set it somewhere to true before the player actually hits the ground and you just constantly keep adding force (pushing up) your player. For more detailed guidance we would need the code that sets your grounded variable to true as well as the order it is called and the place (e.g. if both in Update or...?)
Thanks for the replies, so ive tried moving the script from update to fixedupdate and it causes it to not register button presses and the problem with keeping it in update is the fact that it jumps too high sometimes.At the moment im not home so I cant post the rest of the script but ill post it when i get home.
Well I thought I clearly stated that for your case you should leave the code in Update function. FixedUpdate is used for more constant things like for example running. Why? because by default FixedUpdate is "run" "constant" 50 times/sec (it's not a constant time but for its purposes that doesn't really matter and you should use fixedDeltaTime inside it for ti$$anonymous$$gs, also the number is configurable if needed) while your Update is run as many times as your user machine can handle (if without VSync).
For a better understanding what happens you can change your Get$$anonymous$$eyDown to Get$$anonymous$$ey and your char should jump like a rocket to the stars while you hold Space key ))
Now when he will - re-read again my answer what causes your "jumps too high".
Yeah I get what you mean now. void FixedUpdate () { grounded = Physics2D.OverlapCircle (groundCheck.position, groundRadius, Ground$$anonymous$$ask);
This is wher im setting it, but when I debugged it I could see that the grounded variable is not setting to true of false properly. Sometimes its mid air and its set to true.
Then you have exactly the same issue that I have answered 2 days ago.
If that answer doesn't suit you then you should consider changing the way you detect ground. There are many more ways like Raycasting, Linecasting, OverlapArea, etc. Though all of them will need either precise configuration or some sort of a cooldown.
Your answer
Follow this Question
Related Questions
Problem with animation and void fixedupdate() 1 Answer
counter doesnt work in update 2 Answers
2D Animated tile synchronization 0 Answers
Accelerating speed 2 Answers
2D box collider (character floating) 1 Answer