- Home /
Input.GetKeyUp for Axis input
Hello everyone,
the situation is that i need the player to jump after the space key is released, so this works perfectly
if (Input.GetKeyUp (KeyCode.Space)) {
//do stuff
}
but i need to use custom input instead, i tried doing this :
if (Input.GetAxisRaw("P1Jump")==0) {
//do stuff
}
however the result was obviously the player jumping all the time, what i don't understand is why when i print GetKeyUp i always get false but it is only triggered when i actually press and release the key, but when the Axis Raw it is always zero and it's always calling doing something
thank you
Answer by Jeff-Kesselman · May 04, 2014 at 12:46 AM
Make sure to set your axis type to "KeyOrMOuseButton"
Then read it using Input.GetButtonUp
Answer by AlucardJay · May 04, 2014 at 12:50 AM
http://docs.unity3d.com/Documentation/ScriptReference/Input.GetButtonUp.html
It will not return true until the user has pressed the button and released it again.
This is just how it works. GetKey goes through a cycle of states :
Idle
pressed Down
Held down
released Up
So GetKeyUp is only true after being held down then released, not if it is idle.
You could emulate an axis behaving like a button like this :
#pragma strict
enum AxisState
{
Idle,
Down,
Held,
Up
}
var axisState : AxisState;
var deadZone : float = 0.02;
function Update()
{
switch ( axisState )
{
case AxisState.Idle :
if ( Input.GetAxis( "Horizontal" ) < -deadZone || Input.GetAxis( "Horizontal" ) > deadZone )
{
axisState = AxisState.Down;
}
break;
case AxisState.Down :
axisState = AxisState.Held;
break;
case AxisState.Held :
if ( Input.GetAxis( "Horizontal" ) > -deadZone && Input.GetAxis( "Horizontal" ) < deadZone )
{
axisState = AxisState.Up;
}
break;
case AxisState.Up :
axisState = AxisState.Idle;
break;
}
Debug.Log( axisState );
}
in your example, to check for an axis up state would be :
if (axisState == AxisState.Up) {
//do stuff
}