- Home /
Inup.GetButtonDown not working?
For some reason in my script my Input.GetButtonDown is not working. There are no errors its just when i start the game and press space it just does nothing. Please help p.s. i have a little experience with coding :).
`#pragma strict
var power : float = 500.0;
function OnCollisionEnter(theCollision : Collision) {
if(theCollision.gameObject.name == "Floor") {
Debug.Log("Hit the floor");
}
if(Input.GetButtonDown("Jump")) {
rigidbody.AddForce(Vector3(0, 0, power));
}
}`
Hi. You should use Input classe in Update, LateUpdate, FixedUpdate metod in $$anonymous$$onoBehaviour
Answer by tanoshimi · Jul 19, 2014 at 07:26 AM
That's because your if(Input.GetBurronDown)
test is contained within OnCollisionEnter()
, and so will only ever have an effect if you happen to press space in exactly the same frame as your character enters a collision.....
I suggest you try moving that block of code into Update()
instead, which runs every frame.
yes thank you as soon as i posted this question i fixed it myself but thank you anyway haha.
Answer by robertbu · Jul 19, 2014 at 07:27 AM
You have your Input.GetButtonDown() inside OnCollisionEnter(). This code will only work in the unlikely event that you press the key down at exactly the same frame that the collision occurs. That is Input.GetButtonDown() is only true for a single frame. Assuming you want the key to only be check if there is a collision, then change 'Input.GetButtonDown()' to 'Input.GetButton()'. Note this will still only be true for a single frame (because you are using OnCollisionEnter() which is only true for a single frame).
i worked this out as soon as i posted the question but thank you anyway :)
Your answer
![](https://koobas.hobune.stream/wayback/20220613154450im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
BCE0049 error with network script 0 Answers
Animation spins wildly after completed 0 Answers
Semi colon placement? unity error message 2 Answers
Help with gun scripting!? 1 Answer