- Home /
Get DPad input value via GetButton instead of GetAxis?
Greetings,
I have this generic USB controller. I'm trying to detect its DPad (or analog stick) input values.
Using Input.GetAxis("Horizontal")
and Input.GetAxis("Vertical")
does work (assuming that I set the joystick axis to X for horizontal, and Y for vertical)
I don't want to deal with GetAxis
. Is there anyway I could get the values via Input.GetButton
? The only reserved button names I know for joystick in Unity are joystick button N
where N
could be 0, 1, 2, 3, etc. Is there something like a dpad button 0
?
Any help is appreciated, thanks.
Answer by maggat · Jul 26, 2019 at 11:29 PM
Another solution to this, a bit clunky but it works.
public class Dpad : Monobehaviour {
private float dpadX;
private float dpadY;
private bool leftDpadPressed;
private bool rightDpadPressed;
private bool upDpadPressed;
private bool downDpadPressed;
private bool currentlyReleased;
private void Start(){
currentlyReleased = true;
}
private void Update()
{
dpadX = Input.GetAxis("Dpad X");
dpadY = Input.GetAxis("Dpad Y");
if (dpadX == -1)
{
leftDpadPressed = true;
if(leftDpadPressed && currentlyReleased)
{
//Fire events
print("LEFT");
}
currentlyReleased = false;
}
if(dpadX == 1)
{
rightDpadPressed = true;
if(rightDpadPressed && currentlyReleased)
{
//Fire events
print("RIGHT");
}
currentlyReleased = false;
}
if (dpadY == -1)
{
downDpadPressed = true;
if (downDpadPressed && currentlyReleased)
{
//Fire events
print("DOWN");
}
currentlyReleased = false;
}
if (dpadY == 1)
{
upDpadPressed = true;
if (upDpadPressed && currentlyReleased)
{
//Fire events
print("UP");
}
currentlyReleased = false;
}
if (dpadY == 0 && dpadX == 0)
{
upDpadPressed = false;
downDpadPressed = false;
leftDpadPressed = false;
rightDpadPressed = false;
currentlyReleased = true;
}
}
}
Thankyou for this, it work just like i need. The only thing i had to do is configure the axis in the input manager.
The Vertical axis: Type: Joystick Axis. Axis: 7th Axis (Joysticks). Joynum: Get Motion from all Joysticks. The Horizontal axis: Type: Joystick Axis. Axis: 8th Axis (Joysticks). Joynum: Get Motion from all Joysticks.
Answer by EyePD · Aug 24, 2017 at 02:42 PM
From what I've been reading it seems that Unity does not allow you to read the D-Pad as buttons on Windows (although you can on Mac & Linux). This is really unfortunate because it makes it much harder than it should be to read discrete events from these buttons but I found this other answer which has some (rather clunky) solutions.
Answer by joshuaflash · Aug 24, 2017 at 03:29 PM
I haven't tested it, but using Input.GetAxisRaw might go a long way in getting the behaviour you desire. It doesn't apply smoothing so hitting the D-Pad left (for example) should return -1 immediately.
So something like
bool GetDPadButton(string button)
{
if (button == "right") return Input.GetAxisRaw("DPadX") == 1;
if (button == "down") return Input.GetAxisRaw("DPadY") == -1;
if (button == "left") return Input.GetAxisRaw("DPadX") == -1;
if (button == "up") return Input.GetAxisRaw("DPadY") == 1;
}
could work if the Input settings are properly set up. However, I'm not sure what values are returned when two D Pad buttons are pressed or if there are other cases where one of the axis would return a value between 0 and 1.
Answer by satolas · Feb 14, 2018 at 12:21 AM
DPad X or Y are triggering endless events when they'll rich the value of 1 or -1.
The Input.GetButtonDown/GetButtonDown are giving just one state wich let us to increment a value one by one for exemple. Like a Toggle basically.
I was trying some solutions but no solution is working.
Maybe someone will find a solution for that.
Answer by wallmat · Nov 08, 2018 at 05:43 PM
If anyone is still running into this issue one option is to save the previous state of the axis, and if its the same ignore it. this will allow you to only act on each button press.
var _dir = CrossPlatformInputManager.GetAxis("DPadUpDown");
if(_dir == LastButtonState)
return;
//if its diff then update our last state save that
LastButtonState = _dir;
if(_dir > 0)
//do something awesome
else if(_dir < 0)
//Do something awesome in a diff direction
Your answer
Follow this Question
Related Questions
Input.GetAxis returning nonsensical values for HID Sensor/GameController 0 Answers
Joystick input Problem 3 Answers
Why does holding down any button cause ~20-30 FPS loss? 1 Answer
How can I control cursor with joystick 4 Answers
Map mobile joystick to Input.GetAxis("Horizontal") and "Vertical" 1 Answer