Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Help with custom input code? (individual axis' "buttons")

Discussion in 'Scripting' started by ChadrickEvans, Feb 19, 2018.

  1. ChadrickEvans

    ChadrickEvans

    Joined:
    Mar 14, 2015
    Posts:
    46
    I coded a custom input system that allows me to use axis triggers (for Generic controllers) as buttons. It is activated by checking for a public boolean in my gamepad script. This is what I have so far:


    Code (CSharp):
    1.    
    2. private bool tap;
    3. public bool _GetTapped(int buttonIndex){
    4.         int i = buttonIndex;
    5.         if(Buttons[i].axisEnabled){
    6.             if(Input.GetAxisRaw(Buttons[i].AxisName) == 0 && tap){
    7.                 tap = false; //disable the "tap" bool if the axis is no longer held down
    8.             }
    9.             if(Mathf.Abs(Input.GetAxisRaw(Buttons[i].AxisName)) * invertAxis > 0){
    10.                 if(tap){
    11.                     return false; //So it doesn't continuosly return true
    12.                 }
    13.                 else{
    14.                     tap = true;
    15.                     return true;
    16.                 }
    17.             }
    18.             return false;
    19.         }
    20.         //If the button isn't axis controlled, simply use the joystick button keycode
    21.         return Input.GetKeyDown(buttonKeycodes[i]);
    22.     }
    This allows me to press the axis button and it only returns true once. However, if I check for two different axis-based "buttons" in one update function, only the first "button" is recognized.

    Code (CSharp):
    1.  
    2.         if(gamepad._GetTapped(7) && onGround){
    3.             anim.Play("light attack u");
    4.         }
    5.         if(gamepad._GetTapped(8) && onGround){
    6.             anim.Play("heavy attack u");
    7.         }

    This is because of the tap boolean (I believe). I'm just not sure where to properly place it. My goal is to make each check for an axis "button" individual instead of only one "button" being recognized.

    Is there any way around this? (perhaps using unique ID's for each button press?)
     
  2. takatok

    takatok

    Joined:
    Aug 18, 2016
    Posts:
    1,496
    You could just create an array of booleans one for each button:
    Code (CSharp):
    1. private bool tap[NUM_BUTTONS];
    2. public bool _GetTapped(int buttonIndex){
    3.         int i = buttonIndex;
    4.         if(Buttons[i].axisEnabled){
    5.             if(Input.GetAxisRaw(Buttons[i].AxisName) == 0 && tap[i]){
    6.                 tap[i] = false; //disable the "tap" bool if the axis is no longer held down
    7.             }
    8.             if(Mathf.Abs(Input.GetAxisRaw(Buttons[i].AxisName)) * invertAxis > 0){
    9.                 if(tap[i]){
    10.                     return false; //So it doesn't continuosly return true
    11.                 }
    12.                 else{
    13.                     tap[i] = true;
    14.                     return true;
    15.                 }
    16.             }
    17.             return false;
    18.         }
    19.         //If the button isn't axis controlled, simply use the joystick button keycode
    20.         return Input.GetKeyDown(buttonKeycodes[i]);
    21.     }
     
  3. ChadrickEvans

    ChadrickEvans

    Joined:
    Mar 14, 2015
    Posts:
    46
    I tried this by embedding individual bools into the button class but the issue remains. The animation doesn't play as the button script never gets returned as "true".

    I made a debug.log line just to see if the press was at least recognized, it was. The button script just doesn't get that return.
     
  4. ChadrickEvans

    ChadrickEvans

    Joined:
    Mar 14, 2015
    Posts:
    46
    It seems like it should be very, very simple.

    Code (CSharp):
    1.  
    2. if(Mathf.Abs(Input.GetAxisRaw(Buttons[i].AxisName)) * invertAxis > 0){
    3.           if(Buttons[i].AxisTap){
    4.                return false; //So it doesn't continuosly return true
    5.           }
    6.           else{
    7.               Debug.Log("This is button has been tapped");
    8.               Buttons[i].AxisTap = true;
    9.               return true;
    10.           }
    11. }

    The Debug.log line is right above the line that returns "true" for the entire boolean function. So if the boolean function can print in the debug console why can't it return "true" back to the if statement?