- Home /
W key is unresponsive with input
For some reason the W key only works every once in awhile on every computer but the one I built the project on which it works fine for.
void FixedUpdate ()
{
if (Input.GetKeyDown ("w"))
rigidbody2D.AddForce (Vector2.up * jumpHeight);
}
Yes I know this means the gameobject can jump infinitely but it works fine for this game
If you don't $$anonymous$$d me asking, where did you place this code? Is it in the "Update" function or in a different location? Just seeing the code and knowing that it works occasionally makes me think that you have working code that is placed in the wrong location... Or is imbedded in an if else statement within Update... If we could see more code that would be nice.
There, edited it to include where I made the call, I was told you should always put physics related code into FixedUpdate? I moved it to Update like two of the comments recommended and I am going to test it on another computer in a little bit. The issue I am having is that it works fine on my main laptop I use for things like Unity but as soon as I put onto another computer or laptop the key becomes unresponsive
Answer by Dimling · Oct 22, 2014 at 09:58 AM
There is nothing wrong with the code itself, so it must be the position of the code in the document that is the problem.
I applied the following script to a 2D sprite object and it works fine!
public class TestScript : MonoBehaviour {
public int jumpheight = 25;
void Start()
{
}
void Update()
{
if (Input.GetKeyDown ("w"))
rigidbody2D.AddForce (Vector2.up * jumpheight);
}
}
But remember: The GetDown method returns true during the frame the user starts pressing down the key. So it will only work for one click, or if you press multiple times it will look like a flappy bird game. If you want to get the object to keep moving while holding the button you can for instance use
void Update()
{
if (Input.GetKey ("w"))
rigidbody2D.AddForce (Vector2.up * jumpheight);
}
And just for further explanation: your "Input.Get$$anonymous$$eyDown("w")" part was in a FixedUpdate, which means it only checks once per fixed frame (which is usually around 30-60 per second I believe). $$anonymous$$ost of the time, you do not press the key exactly when it checks, so no input is detected. This is why it sometimes worked and sometimes did not. In the future, I'd advise you put all of your input detection in "Update" so that it is checked every frame.
I'm having this problem too. I put my jump function in Update() and it helped, but it still fails to pick up the key presses sometimes.
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Missed key input 1 Answer
Why Do Inputs Have to Been in Update (Rather than FixedUpdate)? 3 Answers
Distribute terrain in zones 3 Answers
C# Touch Script - Fix GameObject touch. 0 Answers