- Home /
Input: Mixing axis and buttons (ie: Input.GetAxis versus Input.GetButton)
In my game, I use the vertical and horizontal axis (as defined by default in InputManager) in order to control my character.
However, I'd like to be able to use the GetButton functions for the positive/negative keys for the Vertical axis along with using GetAxis to get an analog value. Is there a way to do this?
So in other words, in some parts of my code I do this:
throttle = Input.GetAxis("Vertical") ;
But then I would like to do this, too:
if (Input.GetButtonDown (???))
// where ??? would be Vertical's positive button (plus the alt button)
@chainedlupine This might help for the http://unity3d.com/support/documentation/ScriptReference/Event-alt>alt button. Im not sure what ur saying here "I'd like to be able to use the GetButton functions for the positive/negative keys for the Vertical axis along with using GetAxis" The negative and positive values of GetAxis are -1 and 1.
Can you explain what you are trying to do in more detail.
Just define a new button in the input manager, that takes the same input. Use the axis for the analog stuff, and the button for the toggle stuff.
alt button, as it "alternative button." By default, Vertical is mapped to the up/down arrow keys, plus the W/S keys, plus joystick axis #0.
I know I can get the analog values of that axis by using GetAxis. But I would also like to access the actual button states for the buttons that make up that axis.
So: GetAxis("Vertical") in order to read it as a -1..1 value, or use GetButton("Vertical Positive Button") or GetButton("Vertical Negative Button") in order to treat it like a binary button.
To clarify, currently in my code I operative a vehicle in two modes. One is an airplane mode, where the vertical axis access controls the throttle. But that vehicle also transforms into a walking mode, where vertical should no longer function as a throttle but as a jump button. But I don't want to have a separate Input$$anonymous$$anager define for Jump -- I don't need it, Unity already knows that Vertical axis corresponds to the Up Arrow (aka W key).
I also don't want to use Input.Get$$anonymous$$eyXXX($$anonymous$$eyCode.Up) because that would be a hard-coded dependency upon the Up Arrow. I'd rather use the Input$$anonymous$$anager.
@syclamoth: Yeah I can do it that way but I don't want to, as it means two keybinds to config in the launcher. That could lead to an ugly situation where a user configures the Vertical keybinds but doesn't realize the Jump keybind applies, too.
Since Unity already knows what that key is, if I could just access it then I could use the Get$$anonymous$$ey methods directly.
Answer by chainedlupine · May 05, 2012 at 07:39 PM
I had the idea to just write a button-press simulator on my own, but then I ran into the problem of making sure that I get these changes to my scripts in a timely fashion. Doing a ButtonDown or Up event requires tracking the state of the input across a frame.
So, in short I would do this:
Read state of axis, generate the appropriate button up/down states
Do my usual Update logic that relies upon these states.
Repeat per frame.
Unfortunately, Unity only provides us with a way to delay per-frame processing (via LateUpdate) but no way to do processing before a frame begins. (ie: No PreUpdate.) Therefore, step #1 is never guaranteed to be complete before step #2. (Which can run in any order, AFAIK, though I haven't tested this rigorously but it just makes sense.)
I could do step #1 in step #2 for each time I want to use these inputs, ie: such as...
void Update() { InputLibrary.assignAxisButtonStates ("Vertical") ;
if (InputLibrary.GetAxisAsButton ("Vertical", InputLibrary.UpButton)) { // equiv to Input.GetButton() // ie: Vertical is currently in positive analog range }
if (InputLibrary.GetAxisAsButtonDown ("Vertical", InputLibrary.UpButton)) { // equiv to Input.GetButtonDown() // ie: Vertical was shifted from dead zone to positive analog range since last frame }
if (InputLibrary.GetAxisAsButtonUp ("Vertical", InputLibrary.UpButton)) { // equiv to Input.GetButtonUp() // ie: Vertical was shifted from positive analog range to dead zone since last frame } }
But it's a little ugly. assignAxisButtonStates would need to keep track to make sure it isn't called multiple times per frame.
Answer by cookiepuss · Sep 03, 2012 at 10:31 AM
This works for what you need. This is for a cursor I made.
void Update()
{
if (toggleOn == true)
{
if (Input.GetAxis ("Vertical") > 0)
{
cursormove += 20;
toggleOn = false;
}
if (Input.GetAxis ("Vertical") < 0)
{
cursormove -= 20;
toggleOn = false;
}
}
// do stuff
if (Input.GetAxis ("Vertical") == 0)
{
toggleOn = true;
}
}
Answer by Pandiux · May 05, 2012 at 08:03 AM
Input.GetAxis gives a value more than 0 if being pressed and a negative value if not so you could use
if (Input.GetAxis("Vertical") < 0){
//GO BACK GODE
}
if (Input.GetAxis("Vertical") > 0){
//GO FORWARD GODE
}
or you could do what many people do which is just move to the direction of the axis, for example
transform.Translate(0,0, Input.GetAxis("Vertical");
I know that.
I want to use the GetButtonXXX methods on Input in order to read the binary state of the Vertical axis buttons.
I want to use both Vertical as an analog axis and as an on/off button. See my reply to hijinxbassist above for more detail.
Answer by naglers · Jul 22, 2015 at 07:20 PM
based off of this answer http://answers.unity3d.com/questions/376587/how-to-treat-inputgetaxis-as-inputgetbuttondown.html
You can use Input.GetAxisRaw
to get button states like so:
if(Mathf.Approximately(Input.GetAxisRaw("Horizontal"), -1))
{
//Left
}
else if(Mathf.Approximately(Input.GetAxisRaw("Horizontal"), 1))
{
//Right
}
this works because no smoothing is applied to GetAxisRaw, so if you are holding your key you get that value (I used Mathf.Approx because they are floats and might not be exactly -1/1 just in case)
Your answer
Follow this Question
Related Questions
Press button once to enter vehicle and once again to exit 1 Answer
Microphone input volume to axis value translation 1 Answer
Why does holding down any button cause ~20-30 FPS loss? 1 Answer
Reading 'not defined' Controller Axis Input? 0 Answers
Problem with input code for a camera follow/rotate script 0 Answers