- Home /
2D jump script doesn't work properly.
Hi! I've got a problem with my jump & movement script. Whenever I hit the spacebar my figure doesn't jump properly. It never hits the ground and just "flies" in the air. Does somebody know what I am doing wrong? Any answers appreciated!
Answer by Ermiq · Feb 25, 2021 at 02:39 PM
Input.GetKey()
returns true
in every frame if you hold the button pressed during that frame, and false
only when you don't hold the button down.
Assuming you have your project running at 100-200 frames per second, your code adds jump force to the rigid body multiple times even if you just hit the Space quickly. It might trigger 4 times, 10 times, 50 times in a row, depending on how long the button has been down and how fast the game is rendered.
What to do: use Input.GetKeyDown()
or Input.GetKeyUp()
for non-continious actions. GetKeyDown()
returns true
only once in the one frame within which the button has been pressed down, and then it will be false
until you press it down again. GetKeyUp()
is true
only at the moment when the button has been released.
Your answer
Follow this Question
Related Questions
i need help about players hands positions in 2D multiplayer card game 0 Answers
Input.GetAxisRaw doesnt read Keyboardinput 1 Answer
[2D] Moving the player 1 tile at a time using rigidbody movement 0 Answers
Is there a way to make movement feel movement not feel slippery? 2 Answers
[SOLVED] How to still move the character but with different keys? 0 Answers