Forum rules - please read before posting.

D-Pad as button input inventory menu

edited April 2020 in Technical Q&A

I have my main mechanics set up for my game and before I continue I want to reconsider the controller mapping for all interactions.

I have a HUD element where players will browse through their collected treasure maps. The HUD contains, beside the inventory box, two buttons that use the "Offset Element Slot" to shift to the previous or next item in the list. As long as I link these buttons with the "Alternative input button" option to buttons I defined in the input manager (joystick button 3, 4, 5, etc) everything works fine. But I want this HUD element to be controlled with the d-pad. Main "problem" with d-pads is that they are usually axis instead of buttons.

Thing is, I can't get this to work. I tried several settings within the input manager but no positive result. I also tried to use the Alternative Input Editor to create an action list for this interaction but I have no actions available to work with. I hope someone here can think of an (easy) solution?

Comments

  • Here's a custom scripts that simulates input buttons from an input axis:

    using UnityEngine;
    using AC;
    
    public class SimulateShiftAxis : MonoBehaviour
    {
    
        public string axisName = "ShiftAxis";
        public float minChangeTime = 0.3f;
        public float axisThreshold = 0.4f;
        private float timer;
    
    
        private void Start ()
        {
            KickStarter.playerInput.InputGetButtonDownDelegate += GetButtonDown;
        }
    
    
        private void Update ()
        {
            if (timer > 0f) timer -= Time.deltaTime;
        }
    
    
        private bool GetButtonDown (string axis)
        {
            try
            {
                if (axis == "ShiftLeft")
                {
                    float scrollwheel = Input.GetAxisRaw (axisName);
                    if (scrollwheel > axisThreshold)
                    {
                        if (timer <= 0f)
                        {
                            timer = minChangeTime;
                            return true;
                        }
                    }
                }
                else if (axis == "ShiftRight")
                {
                    float scrollwheel = Input.GetAxisRaw (axisName);
                    if (scrollwheel < -axisThreshold)
                    {
                        if (timer <= 0f)
                        {
                            timer = minChangeTime;
                            return true;
                        }
                    }
                }
    
                return Input.GetButtonDown (axis);
            }
            catch {}
    
            return false;
        }
    
    }
    

    There's three axes names in the script here: ShiftLeft, ShiftRight, and Shift. You'll need to rename these to what you have defined.

    ShiftLeft and ShiftRight refer to the inputs listed in the "Alternative input button" fields for the actual Button elements. Shift refers to the d-pad axis, which is 2D so you'll need to define positive and negative buttons for those in Unity's Input Manager.

  • Thanks Chris! Just a quick up date: i got the script/component working and I can at least browse through the items. Only problem now is dat everything is extremely finicky and unpredictable (items sometimes jumps two at a time, up becomes left, right becomes up, etc).

    My thinking right now is I have to dive a bit deeper into tweaking the axis settings in the input manager (sensitivty, dead zones, etc). I'm trying to use a joystick as buttons so I'm pretty sure I need to adjust accordingly.

    Once again thanks for the script!!!

  • That method relies on a timer - so that keeping the axis held will cause it to have an effect every few frames.

    An alternative method, whereby you need to let go of the axis for it to be able to have an effect again:

    using UnityEngine;
    using AC;
    
    public class SimulateShiftAxis : MonoBehaviour
    {
    
        public string axisName = "ShiftAxis";
        public float axisThreshold = 0.4f;
        private bool isLocked;
    
    
        private void Start ()
        {
            KickStarter.playerInput.InputGetButtonDownDelegate += GetButtonDown;
        }
    
    
        private bool GetButtonDown (string axis)
        {
            try
            {
                float scrollwheel = Input.GetAxisRaw (axisName);
                if (axis == "ShiftLeft")
                {
                    if (!isLocked && scrollwheel > axisThreshold)
                    {
                        isLocked = true;
                        return true;
                    }
                }
                else if (axis == "ShiftRight")
                {
                    if (!isLocked && scrollwheel < -axisThreshold)
                    {
                        isLocked = true;
                        return true;
                    }
                }
    
                if (scrollwheel < (axisThreshold / 2f) && scrollwheel > -(axisThreshold / 2f))
                {
                    isLocked = false;
                }
    
                return Input.GetButtonDown (axis);
            }
            catch {}
    
            return false;
        }
    
    }
    
  • Yes! This script gives much more stability, thanks again Chris!

    I also start to believe that the axis on the d-pad of the Xbox One S controller might use the same for both vertical and horizontal. The only input in the input manager that is using the 5th axis is the DpadHorizontal I defined... yet I can also browse through my inventory with left and right as well as up and down.

    I can find next to nothing about this specific controller. I even thought that maybe my dpad was broken but the other Xbox One S controller acts exactly the same. Anybody has any idea what might be going on? I'm gonna try and hook up a PS4 controller as well, see if the problem prevails.

Sign In or Register to comment.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Welcome to the official forum for Adventure Creator.