I want to do something in the first frame in which a button is pressed, but what button I want to check depends on what button is mapped to a given input axis. Is there a way to pass "the button mapped to this axis" to Input.KeyDown and/or Input.ButtonDown, rather than a predetermined button? Alternatively, is there an "AxisDown" function or something similar that returns true the first frame that an axis is pressed?
There is no such function that I know of. You will have to check it yourself.
//Checks if an axis is in motion
var barPressed ; boolean = false;
function Update() {
var foo = Input.GetAxisRaw("bar");
if(foo != 0 && !barPressed) { //AxisDown
DoSomething();
barPressed = true;
}
else if(foo == 0 && barPressed) { //AxisUp
DoSomethingElse();
barPressed = false;
}
}
If you have one check, you need the other to flip the flag back.
If you mean to check for a specific button of the axis, then you have to be even more explicit with your checking.
//Checks if an axis is in motion
var barDownPressed ; boolean = false;
var barUpPressed ; boolean = false;
function Update() {
var foo = Input.GetAxisRaw("bar");
if(foo < 0) {
if(!barDownPressed) { //AxisDownDown
DoSomething();
barDownPressed = true;
}
if(barUpPressed) { //AxisUpUp
DoSomethingElse();
barUpPressed = false;
}
}
else if(foo > 0) {
if(barDownPressed) { //AxisDownUp
DoSomethingElseDifferent();
barDownPressed = false;
}
if(!barUpPressed) { //AxisUpDown
DoSomethingMore();
barUpPressed = true;
}
}
else if(foo == 0) {
if(barUpPressed)) { //AxisUpUp
DoSomethingElse();
barUpPressed = false;
}
if(barDownPressed) { //AxisDownUp
barDownPressed = false;
DoSomethingElseDifferent();
}
}
}
You pass Axis names to Input.GetButton… methods, such as Input.GetButtonDown.
So wherever Input.GetButtonDown works, it functions as any “Input.GetAxisDown(…) > 0” would if such a method existed.
If you need more distinction between keys than that, then separate the keys into separate input axes.