x


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)
more ▼

asked Mar 14 '10 at 12:20 PM

MedianHansen gravatar image

MedianHansen
63 2 4 13

(comments are locked)
10|3000 characters needed characters left

1 answer: sort voted first

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.

more ▼

answered Mar 14 '10 at 12:32 PM

duck gravatar image

duck ♦♦
41k 92 148 415

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 ^^

Mar 14 '10 at 01:20 PM MedianHansen

@MedianHansen 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

Mar 14 '10 at 02:34 PM Lipis
(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x1949
x1672
x657
x166

asked: Mar 14 '10 at 12:20 PM

Seen: 2034 times

Last Updated: Mar 18 '10 at 12:26 PM