This is just a sort of an update question as there already have been a couple of the like, but all some years old.
So: is there a “intended” way of getting the joystick D-Pad as buttons instead of axis (e.g. for using GetButtonDown()).
I just can’t get my head around this not already being a thing.
As far as I can remember, using the Dpad or Joystick exactly as you would use a button won’t work.
Simply because instead of having two states (pressed or not), an axis can have thousands of values between -1 and 1. (Or 0 and 1)
What you can do is say:
• If the horizontal axis is at less than -0.75f, then it’s a left Dpad press.
• If the horizontal axis is at more than 0.75f, then it’s a right Dpad press.
• Else, between -0.75f and 0.75f the buttons are not being pressed.
Pair that with a few bools, and you can have a system that’s equivalent to Input.GetKey, GetKeyDown and GetKeyUp.
You can find a little more on this thread:
Also, it’s up to you to define and test how high the sensitivity value (that -0.75f and 0.75f) should be, i.e. how hard your players need to press the Dpad for a button press to trigger.
You are going to create a game object and call it dPadTranslator. In it you are going to add a new script, call this DPadTranslator. Once you get this open up the script:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class DPadTranslator : MonoBehaviour
{
public int up_buttonDown;
[SerializeField] private bool up_chkDown;
private void Update() {
if(Input.GetAxis("vertical") > 0 && !up_chkDown) {
up_chkDown = true;
StartCoroutine(Up_ButtonDownCo());
} else if(Input.GetAxis("vertical") == 0) {
up_chkDown = false;
}
}
IEnumerator Up_ButtonDownCo() {
up_buttonDown = 1;
yield return new WaitForEndOfFrame();
up_buttonDown = 0;
}
}
Now pay attention: the idea is to emulate the ButtonDown, so when you push the dPad down it will send a 1 for the rest of the frame and then it will send a 0. Until you release the button. This will act like Input.GetButtonDown.
Now, in the object you want to behave like you have pressed the button instead of an axis, you will read the up_buttonDown variable, that’s why it’s public.
In other words, you will not read directly the input but the translator of the input which will give you a short signal for the end of the frame.
This script works for the up button so you have to configure it in the project settings as "vertical" and assign it to the 8-axis. So:
go to Edit → Project Settings → InputManager
and create a new input. Set the name to "vertical", Type JoyStick Axis, Axis 8-Axis.
Once you test this work rewrite the code so it can handle the other buttons. You just have to repeat the if block in the update three more times, once for each button and the same for the coroutines. Change the names of the variables to left, down, etc, as well as the coroutine.
@MarekLg@vdarned I do this often. Just set a bool and disable it once it’s used:
bool dpadVerticalBlocked = false;
//If they hit full dpad and its allowed, do stuff
if ( (Input.GetAxis("DPadVertical") ==1) && (dpadVerticalBlocked==false) ) {
//Do your button down stuff here
dpadVerticalBlocked=true; //Disable it
} else {
//if they release, allow them to hit it again.
if ( Input.GetAxis("DPadVertical") ==0) { dpadVerticalBlocked=false;}
}