translate this page

20170109

Testing GetKey(), GetKeyDown(), GetKeyUp()





 I tested the three key-reading methods - Input.GetKey(), Input.GetKeyDown(), and Input.GetKeyUp() - to check when they return true, by simply putting Debug.Log() lines in Unity Editor Ver.5.5.0f3.


private int FrameCount;

private void Update()
{
    Framecount++;

    if(Input.GetKey(KeyCode.Space))
    {
        Debug.Log(string.Format("-- GetKey @FrameNumber:{0}" , Framecount));
    }
    if(Input.GetKeyDown(KeyCode.Space))
    {
        Debug.Log(string.Format("__ GetKeyDown @FrameNumber:{0}", Framecount));
    }
    if(Input.GetKeyUp(KeyCode.Space))
    {
        Debug.Log(string.Format("|| GetKeyUp @FrameNumber:{0}", Framecount));
    }

}


And the below image is what I could see in the editor's log window.




According to the log :
1. GetKeyDown() returns true at the frame where the specified key is 'freshly' pressed down.
2. GetKey() returns true when the key is in 'pressed' state at the frame.
3. GetKeyUp() returns true at the frame where the key is released.

By 1 and 2, two log messages were written at the same frame.
By 2, while holding the key down, GetKey() returns true.

:)

No comments :

Post a Comment

Popular Posts