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

C# Xbox One controller d-pad for PC work like a button "GetButtonDown" [SOLVED]

Discussion in 'Scripting' started by ralf_b, Jul 3, 2017.

  1. ralf_b

    ralf_b

    Joined:
    Jul 9, 2013
    Posts:
    48
    Hello everybody,

    I would like to use the d-pad up and down of my Xbox one controller like buttons, so that they fire only the first frame as long as they are pressed.
    Although this seems to have been discussed a lot of times already I can not get this to run properly. The cube still moves in X / Y direction as long as any of the buttons is pressed.

    Does anyone spot my error or have a solution to this?


    Code (CSharp):
    1. public class test_move : MonoBehaviour {
    2.     public float p1_horizontal; // save d-pad values
    3.     public float p1_vertical; // save d-pad values
    4.  
    5.     public bool p1_up = false;
    6.     public bool p1_down = false;
    7.     public bool p1_left = false;
    8.     public bool p1_right = false;
    9.  
    10.     public bool p1_horizontal_pressed = false;
    11.     public bool p1_vertical_pressed = false;
    12.  
    13.     public float speed = 11f;
    14.  
    15.  
    16.     void Update () {
    17.         p1_horizontal = Input.GetAxisRaw("p1_horizontal");
    18.         if (p1_horizontal < 0) {
    19.             p1_left = true;
    20.             p1_right = false;
    21.         } else if (p1_horizontal > 0) {
    22.             p1_left = false;
    23.             p1_right = true;
    24.         } else {
    25.             p1_left = false;
    26.             p1_right = false;
    27.         }
    28.  
    29.         p1_vertical = Input.GetAxisRaw("p1_vertical");
    30.         if (p1_vertical > 0 && p1_vertical_pressed == false) {
    31.             p1_vertical_pressed = true;
    32.             p1_up = true;
    33.             p1_down = false;
    34.         } else if (p1_vertical < 0 && p1_vertical_pressed == false) {
    35.             p1_vertical_pressed = true;
    36.             p1_up = false;
    37.             p1_down = true;
    38.         } else {
    39.             p1_vertical_pressed = false;
    40.             p1_up = false;
    41.             p1_down = false;
    42.         }
    43.  
    44.  
    45.             transform.Translate(p1_horizontal * Time.deltaTime * speed, p1_vertical * Time.deltaTime * speed, 0);
    46.     }
    47. }
     
  2. Khazmadu

    Khazmadu

    Joined:
    Jun 24, 2017
    Posts:
    92
    Try to make it like this:

    Code (CSharp):
    1.     bool upIsPressed = false;
    2.     bool downIsPressed = false;
    3.     bool isNeutral = true;
    4.  
    5.     void Update () {
    6.  
    7.         float horizontal = Input.GetAxis("P1_Horizontal");
    8.  
    9.         if (horizontal < 0 && !downIsPressed)
    10.         {
    11.             isNeutral = false;
    12.             downIsPressed = true;
    13.             YourDownFunction();
    14.         }else if (horizontal > 0 && !upIsPressed)
    15.         {
    16.             isNeutral = false;
    17.             upIsPressed = true;
    18.             YourUpFunction();
    19.         }
    20.         else if (horizontal == 0 && !isNeutral)
    21.         {
    22.             isNeutral = true;
    23.             downIsPressed = false;
    24.             upIsPressed = false;
    25.         }
    This is just one Horizontal but you can do it just like that for vertical.
     
  3. ralf_b

    ralf_b

    Joined:
    Jul 9, 2013
    Posts:
    48
    OH my goddness I just realized I am using the Input.GetAxisRaw for the translate directly, so no wonder it is exeuted every frame regardless everything else *facepalm*

    @Khazmadu thanks for the hint with doing the translate in a separate function :)
    I got to rework this when I am back home and post the working code here.
     
  4. radwan92

    radwan92

    Joined:
    Sep 6, 2013
    Posts:
    56
    Also notice that @Khazmadu 's script has an else-if in the last part, which is a crucial fix. Without it the translation would happen every other frame.
     
  5. ralf_b

    ralf_b

    Joined:
    Jul 9, 2013
    Posts:
    48
    Finally got it running (I should not do such things after 11pm, I kept on mixing up variables).

    Anyhow here goes my odd target to move something with a d-pad smoothely left and right but only in steps up and down

    Code (CSharp):
    1. public float p1_horizontal; // save d-pad values
    2.     public float p1_vertical; // save d-pad values
    3.  
    4.     public bool leftIsPressed = false;
    5.     public bool rightIsPressed = false;
    6.  
    7.     public bool vertNeutral = true;
    8.     public bool upIsPressed = false;
    9.     public bool downIsPressed = false;
    10.  
    11.     public float speed = 11f;
    12.  
    13.  
    14.     void Update() {
    15.         p1_horizontal = Input.GetAxis("p1_horizontal");
    16.         if (p1_horizontal < 0) {
    17.             leftIsPressed = true;
    18.             p1_horiz_move();
    19.         } else if (p1_horizontal > 0) {
    20.             rightIsPressed = true;
    21.             p1_horiz_move();
    22.         } else if (p1_horizontal == 0) {
    23.             leftIsPressed = false;
    24.             rightIsPressed = false;
    25.         }
    26.  
    27.         p1_vertical = Input.GetAxisRaw("p1_vertical");
    28.         if (p1_vertical < 0 && !downIsPressed) {
    29.             vertNeutral = false;
    30.             downIsPressed = true;
    31.             p1_vert_move();
    32.         } else if (p1_vertical > 0 && !upIsPressed) {
    33.             vertNeutral = false;
    34.             upIsPressed = true;
    35.             p1_vert_move();
    36.         } else if (p1_vertical == 0 && !vertNeutral) {
    37.             vertNeutral = true;
    38.             downIsPressed = false;
    39.             upIsPressed = false;
    40.         }
    41.  
    42.  
    43.     }
    44.  
    45.     void p1_horiz_move() {
    46.         transform.Translate(Vector3.right * p1_horizontal * Time.deltaTime * speed);
    47.     }
    48.  
    49.     void p1_vert_move() {
    50.         transform.Translate(Vector3.up * p1_vertical * Time.deltaTime * speed);
    51.     }