Calling GetButtonDown on an input axis
Hi, this is (in pseudocode) the behaviour I want to create:
if (Input.getButtonDown( someAxis.positiveButton )) //do smoething
if (Input.getButtonDown( someAxis.negativeButton )) //do smoething else
I found on another thread following solution:
if (Input.GetButtonDown("someAxis") && Input.GetAxisRaw("someAxis") > 0) //do something
if (Input.GetButtonDown("someAxis") && Input.GetAxisRaw("someAxis") < 0) //do something else
But this doesn't work for following case: Let's say I keep "A" pressed cosntantly and while keeping A pressed I press D every now end then. I want to know every time that D is pressed even if A is pressed at the same time, but GetAxisRaw will only go to 0, so the corresponding function won't trigger.
How do I trigger an action every time the positive or negative Key of an Axis are pressed?
Answer by Helliaca · Sep 07, 2016 at 03:06 PM
I had the idea of checking the raw axis value of the previous frame. So this is the monstruousity of an answer I've come up with:
if(Input.GetButtonDown("Horizontal")) {
if(Input.GetAxisRaw("Horizontal") > 0) print("D pressed");
else if(Input.GetAxisRaw("Horizontal") < 0) print("A pressed");
else if(prev < 0) print("D pressed");
else if(prev > 0) print("A pressed");
}
prev = Input.GetAxisRaw("Horizontal");
Im sure there are more elegant and efficient ways of doing it, but it does work at least.
Your answer
Follow this Question
Related Questions
GetAxis help? 1 Answer
Why isn't my basic input script working? 0 Answers
Problems with touch input. 0 Answers
Input.GetKeyDown not reading? Am I being oblivious? 1 Answer
Moving relative to camera 0 Answers