- Home /
Jumping to add force but stop after a while
Hello I'm Leonardo, english is not my native language and I'm new to coding so i hope you will understand.
I'm trying to make my character jump when I hold the button and add a negative force on the y axis when i release.
I tried saying this:
if (Input.GetButton) { //if you hold the button
rigidbody2D.AddForce (new Vector2 (0, jumpForce));
}
if (Input.GetButtonUp) {
rigidbody2D.AddForce (new Vector2 (0, -jumpForce));
}
But the result was that the character goes up as long as i hold the button, but i want it to go up and after 1 second (or less) to go down even if i hold the button for an infinite amount of time.
So my question is:
Can I add a force and after a while stop adding it? If not, what else can I do to have the same effect?
Thanks in advance!
oh and forgot to say c# only
use Input.GetButtonDown ins$$anonymous$$d of Input.GetButton you not need Input.GetButtonUp then
Rather than adding the regular force add Impulse force on key down.
if(Input.GetButtonDown("jump"))
rigidbody2D.AddForce (new Vector2 (0, jumpForce), Force$$anonymous$$ode.Impulse);
Answer by Leonardo_Temperanza · Aug 18, 2014 at 08:19 PM
When I wrote the code it gave me an error in "ForceMode.Impulse", but now I figured out I had to write "ForceMode2D.Impulse" and it works perfectly. Thanks! :]
Your answer
![](https://koobas.hobune.stream/wayback/20220613160901im_/https://answers.unity.com/themes/thub/images/avi.jpg)