- Home /
Odd input glitch
I have a script that toggles the drag of a physics object while x is held down and then when x is released it returns the drag to 0. The problem is that sometimes the key presses don't register, sometimes it wont register that the button is pressed and other times it won't recognize the key is released anyone see a problem with my code? Here is the related code:
function FixedUpdate () {
if(Input.GetKeyDown("x")){
Brake(true);
}
if(Input.GetKeyUp("x")){
Brake(false);
}
}
function Brake (Down){
if(Down){
rigidbody.drag = 1;
}
else{
rigidbody.drag = 0;
}
}
Answer by BigRoy · Jan 19, 2014 at 10:03 PM
Use the Update() method instead of FixedUpdate().
The documentation states:
You need to call this function from the Update function, since the state gets reset each frame. It will not return true until the user has released the key and pressed it again.
http://docs.unity3d.com/Documentation/ScriptReference/Input.GetKeyDown.html
See also:
http://answers.unity3d.com/questions/302814/placing-inputgetkeydown-inside-fixedupdate-slows-d.html
http://answers.unity3d.com/questions/270552/not-sure-if-this-should-go-in-update-or-fixedupdat.html
http://answers.unity3d.com/questions/256271/intermittent-detection-of-input-in-fixedupdate.html
http://stackoverflow.com/questions/19259097/is-it-really-wrong-to-use-input-getkey-on-fixedupdate
I have tried that before, no effect whatsoever. Edit, Never$$anonymous$$d you are right it seems when I did that before i must not have saved the script so I saw no changes FacePalm
Answer by Yusaku · Jan 19, 2014 at 09:06 PM
I think you have some extra {and} in there could be wrong but thats what it looks like. Fix that first and see if it helps. Sorry my bad i was wrong. I dont know whats going on!
Thanks for trying, this is getting annoying ahh I really hate bugs