- Home /
Why is GetKeyDown / Up behaving erratically, only responding occasionally in build?
Hi, i'm very new to unity, and as such, i was gonna make a basic demo, just to increase my skills, it should make the player capable of picking up certain items, and activating others, etc. etc.
Now, the key i used for picking up items was the 'E' key, like in many FPS. Being new, i just edited the "FPSWalker" script, which is a build-in, at least in my version of unity.
When testing and all that, it executed just fine, everything worked. I then tried to build it, and run it, and now the 'E' key seemed to only pick up the items once in a while. I then added the line: MoveDirection.y=jumpSpeed as a consequence for pressing the 'E' key, and still it only jumped sometimes, no saying when. The strange thing is still that in the unity editor, it all works fine, but as soon as i build it, the 'E' key only reacts sometimes, while any other key in the game (Which was already written in the build-in-script, not by me), works perfectly, and no lag is present, so it can't be any of those options, my script is as following:
var speed = 6.0; var jumpSpeed = 8.0; var gravity = 20.0; var gem; var altar; var gui; var Offer1=0;
private var moveDirection = Vector3.zero; private var grounded : boolean = false;
function Start () { gem=GameObject.Find("Obj1"); altar=GameObject.Find("Altar"); gui=GameObject.Find("GUI Text");
}
function FixedUpdate() { if (grounded) { // We are grounded, so recalculate movedirection directly from axes moveDirection = new Vector3(Input.GetAxis("Horizontal"), 0, Input.GetAxis("Vertical")); moveDirection = transform.TransformDirection(moveDirection); moveDirection *= speed;
if (Input.GetButton ("Jump")) {
moveDirection.y = jumpSpeed;
}
}
// Apply gravity
moveDirection.y -= gravity * Time.deltaTime;
if (Input.GetKeyDown(KeyCode.E))
{
moveDirection.y = jumpSpeed;
if (gem)
{
if (Vector3.Distance(transform.position,gem.transform.position)<2.5)
if (gem.GetComponent("MouseOver").MO==1)
{
Offer1=1.0;
Destroy(gem);
}
}
if (altar)
if (Vector3.Distance(transform.position,altar.transform.position)<3.2)
if (altar.GetComponent("MouseOver").MO==1)
if (Offer1==1)
{
gui.GetComponent("ShowLife").LifeUp(10);
Offer1=0;
}
}
// Move the controller
var controller : CharacterController = GetComponent(CharacterController);
var flags = controller.Move(moveDirection * Time.deltaTime);
grounded = (flags & CollisionFlags.CollidedBelow) != 0;
}
@script RequireComponent(CharacterController)
Answer by duck · Mar 14, 2010 at 12:32 PM
It's because you're reading it in FixedUpdate. You should probably move the section of code relating to jumping to the Update function.
This is because GetKeyDown is true only for that frame where the key is pressed, so it's inherently linked to the frame rate (and therefore the framerate-based "Update()" function) rather than the physics-engine-based "FixedUpdate()" function. As a result, KeyDown or KeyUp input readings can be missed if you use it in FixedUpdate. The degree or frequency to which they're missed will be related to the framerate, so that is probably why you're seeing a difference between the performance in the editor compared with the built version.
thanks a lot, i was wondering what the difference between those two functions were. I tried moving it to a Update() function, and it worked perfectly, thanks for the help once again ^^
@$$anonymous$$edianHansen Don't forget to accept the answer if it was helpful.. (the little check-mark, next to the answer..) Also if you haven't already did it.. you have to read this: http://answers.unity3d.com/faq
Your answer
Follow this Question
Related Questions
How to exclude "Selection & Handles" and other editor-specific code in builds? 2 Answers
Unity game crashes when build but not in editor 0 Answers
DLL build issues with Unity 3 1 Answer
GUI Question 0 Answers
Distribute terrain in zones 3 Answers