Hi, I am pretty new to C# and Unity, and I am creating a Script from which I can check if the axes of the dpad have been pressed or or is held, the below code works for this. However I also wanted the script to realize if the button has been released after being held, Is there a way I can do this in a simple way.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Controller : MonoBehaviour {
public float DPADVERT;
public float DPADHOR;
public float LAVERT;
public float LAHOR;
public float RAVERT;
public float RAHOR;
public static bool DPadUpPressed;
public static bool DPadDownPressed;
public static bool DPadLeftPressed;
public static bool DPadRightPressed;
public static bool DPadUpReleased;
public static bool DPadDownReleased;
public static bool DPadLeftReleased;
public static bool DPadRightReleased;
public static bool DPadUpHold;
public static bool DPadDownHold;
public static bool DPadLeftHold;
public static bool DPadRightHold;
public static bool DPadUpPressed2;
public static bool DPadDownPressed2;
public static bool DPadLeftPressed2;
public static bool DPadRightPressed2;
public static bool LAUpPressed;
public static bool LADownPressed;
public static bool LALeftPressed;
public static bool LARightPressed;
public static bool LAUpHold;
public static bool LADownHold;
public static bool LALeftHold;
public static bool LARightHold;
public static bool LAUpPressed2;
public static bool LADownPressed2;
public static bool LALeftPressed2;
public static bool LARightPressed2;
public static bool RAUpPressed;
public static bool RADownPressed;
public static bool RALeftPressed;
public static bool RARightPressed;
public static bool RAUpHold;
public static bool RADownHold;
public static bool RALeftHold;
public static bool RARightHold;
public static bool RAUpPressed2;
public static bool RADownPressed2;
public static bool RALeftPressed2;
public static bool RARightPressed2;
void Start () {
}
// Update is called once per frame
void Update () {
DPADVERT = Input.GetAxisRaw("DVERT");
DPADHOR = Input.GetAxisRaw("DHOR");
LAVERT = Input.GetAxisRaw("LAVERT");
LAHOR = Input.GetAxisRaw("LAHOR");
RAVERT = Input.GetAxisRaw("RAVERT");
RAHOR = Input.GetAxisRaw("RAHOR");
//DPAD Vertical
if (DPADVERT==-1){
DPadUpHold=true;
}
else {
DPadUpHold=false;
}
if (DPADVERT==-1&&!DPadUpPressed2){
DPadUpPressed=true;
DPadUpPressed2=true;
}
else{
DPadUpPressed=false;
}
if(!DPadUpPressed &&DPADVERT!=-1){
DPadUpPressed2=false;
}
if (DPADVERT==1){
DPadDownHold=true;
}
else {
DPadDownHold=false;
}
if (DPADVERT==1&&!DPadDownPressed2){
DPadDownPressed=true;
DPadDownPressed2=true;
}
else{
DPadDownPressed=false;
}
if(!DPadDownPressed &&DPADVERT!=1){
DPadDownPressed2=false;
}
//DPAD Horizontal
if (DPADHOR==-1){
DPadLeftHold=true;
}
else {
DPadLeftHold=false;
}
if (DPADHOR==-1&&!DPadLeftPressed2){
DPadLeftPressed=true;
DPadLeftPressed2=true;
}
else{
DPadLeftPressed=false;
}
if(!DPadLeftPressed &&DPADHOR!=-1){
DPadLeftPressed2=false;
}
if (DPADHOR==1){
DPadRightHold=true;
}
else {
DPadRightHold=false;
}
if (DPADHOR==1&&!DPadRightPressed2){
DPadRightPressed=true;
DPadRightPressed2=true;
}
else{
DPadRightPressed=false;
}
if(!DPadRightPressed &&DPADHOR!=1){
DPadRightPressed2=false;
}
//LeftAnalog Controls
//Vertical
//Up
if (LAVERT==-1){
LAUpHold=true;
}
else {
LAUpHold=false;
}
if (LAVERT==-1&&!LAUpPressed2){
LAUpPressed=true;
LAUpPressed2=true;
}
else{
LAUpPressed=false;
}
if(!LAUpPressed &&LAVERT!=-1){
LAUpPressed2=false;
}
//Down
if (LAVERT==1){
LADownHold=true;
}
else {
LADownHold=false;
}
if (LAVERT==-1&&!LADownPressed2){
LADownPressed=true;
LADownPressed2=true;
}
else{
LADownPressed=false;
}
if(!LADownPressed &&LAVERT!=-1){
LADownPressed2=false;
}
//Horizontal
//Left
if (LAHOR==-1){
LALeftHold=true;
}
else {
LALeftHold=false;
}
if (LAVERT==-1&&!LALeftPressed2){
LALeftPressed=true;
LALeftPressed2=true;
}
else{
LALeftPressed=false;
}
if(!LALeftPressed &&LAVERT!=-1){
LALeftPressed2=false;
}
//Right
if (LAHOR==1){
LARightHold=true;
}
else {
LARightHold=false;
}
if (LAVERT==-1&&!LARightPressed2){
LARightPressed=true;
LARightPressed2=true;
}
else{
LARightPressed=false;
}
if(!LARightPressed &&LAVERT!=-1){
LARightPressed2=false;
}
//RightAnalog Controls
//Vertical
//Up
if (RAVERT==-1){
RAUpHold=true;
}
else {
RAUpHold=false;
}
if (RAVERT==-1&&!RAUpPressed2){
RAUpPressed=true;
RAUpPressed2=true;
}
else{
RAUpPressed=false;
}
if(!RAUpPressed &&RAVERT!=-1){
RAUpPressed2=false;
}
//Down
if (RAVERT==1){
RADownHold=true;
}
else {
RADownHold=false;
}
if (RAVERT==-1&&!RADownPressed2){
RADownPressed=true;
RADownPressed2=true;
}
else{
RADownPressed=false;
}
if(!RADownPressed &&RAVERT!=-1){
RADownPressed2=false;
}
//Horizontal
//Left
if (RAHOR==-1){
RALeftHold=true;
}
else {
RALeftHold=false;
}
if (RAVERT==-1&&!RALeftPressed2){
RALeftPressed=true;
RALeftPressed2=true;
}
else{
RALeftPressed=false;
}
if(!RALeftPressed &&RAVERT!=-1){
RALeftPressed2=false;
}
//Right
if (RAHOR==1){
RARightHold=true;
}
else {
RARightHold=false;
}
if (RAVERT==-1&&!RARightPressed2){
RARightPressed=true;
RARightPressed2=true;
}
else{
RARightPressed=false;
}
if(!RARightPressed &&RAVERT!=-1){
RARightPressed2=false;
}
}
}
PS.Ignore the LA and RA variables those are for Left and Right Analog Sticks.
Also the Variables DPadUpReleased have been provided so that it becomes easier for me to understand, you may change them if you want, but do mention them.
In this code DVERT AND DHOR are the vertical and horizontal axes of the dpad respectively.
IK this code is very long but only a third of it is used for the dpad, once ik how to detect dpadbuttons being released I will use the same for LA and RA.
Sorry if this is Inconvenient and tiring to look at.