Trouble Making D-pad Double Tap...

I am working on a top down 2D shooter and want the player to be able to double tap in a direction to boost/dodge. I have cobbled together some code in C# to accomplish this and when I use my keyboard controls it works fine. Unfortunately I can not say the same about my xbox 360 d-pad controls. For some reason the double tap does not work on the D-pad. I am not sure why and I am an amateur when it comes to coding, so I could use a little help.

Here is what I have so far:

public float doubleTapTime;
public float playerDefaultSpeed;
public float playerSpeed;
public float playerDodgeSpeed;
public float HorizontalMove;
public float VerticalMove;

private float lastTapTimeV = 0;
private float lastTapTimeH = 0;

    // Use this for initialization
    void Start()

    {
        //Start position    

        transform.position = new Vector3(0, -3, transform.position.z);   
    }

    // Update is called once per frame
    void Update()

    {
    //Controls//Movement

    //Movement
        HorizontalMove = Input.GetAxisRaw("Horizontal") * playerSpeed * Time.deltaTime;
        transform.Translate(Vector3.right * HorizontalMove);

        VerticalMove = Input.GetAxisRaw("Vertical") * playerSpeed * Time.deltaTime;
        transform.Translate(Vector3.up * VerticalMove);


        //Dodge Vertical
    if (Input.GetButtonUp("Vertical"))
       playerSpeed = playerDefaultSpeed;

    if (Input.GetButtonDown("Vertical"))
    {
       if (Time.time - lastTapTimeV < doubleTapTime)
       {
         lastTapTimeV = Time.time;
         playerSpeed = playerDodgeSpeed;
       } 

       else 
       {
         lastTapTimeV = Time.time;
         playerSpeed = playerDefaultSpeed;
       }

    if (Input.GetButtonUp("Vertical"))
       playerSpeed = playerDefaultSpeed; 
    }  

    //Dodge Horizontal
    if (Input.GetButtonUp("Horizontal"))
       playerSpeed = playerDefaultSpeed;

    if (Input.GetButtonDown("Horizontal"))
    {
       if (Time.time - lastTapTimeH < doubleTapTime)
       {
         lastTapTimeH = Time.time;
         playerSpeed = playerDodgeSpeed;
       } 

       else 
       {
         lastTapTimeH = Time.time;
         playerSpeed = playerDefaultSpeed;
       }

    if (Input.GetButtonUp("Horizontal"))
       playerSpeed = playerDefaultSpeed; 
    }

I am using the inputs manager in Unity and here are some pics of that setup:


I thought it might have something to do with the D-pad being analogue vs. digital, but I made the HorizontalMove and VerticalMove variables public and it looked like the same values were getting placed in them regardless of weather I used the keyboard or the D-pad.

Thanks in advance for any help or suggestions.

The d-pad or the thumbstick? Because a d-pad isn’t analogue, it’s just 4 buttons in a cross shape.

That aside, if it is the thumbstick you’re talking about, then I assume the GetButtonXYZ(…) functions won’t do anything, because even though it has the same name it’s not a button as such.

I am talking about the D-pad. I initially thought it was digital, but based on what I read while researching my problem and the range of values it was placing in my variables I think it may give more than just a simple on/off response. I could be totally wrong about that, but either way I did not want to discount it as the root of my problem.

Interesting. What gamepad are you using, out of interest?

I forget the details, but Unity will smooth the values of digital input according to the “gravity” and “sensitivity” parameters in the Input dialog showing above. From memory, the “snap” setting disables that and just gives you the raw input. I haven’t had to play with it for a long time, though, so I could be getting that wrong. Anyway, that could be another explanation as to why it’s placing analogue-esque values in your variables.

I suggest sticking a few prints in your code so that you can see when each input makes each part of the code run.

I am using the Xbox Windows Live gamepad. I did try messing around with the “gravity” and “sensitivity” but may not have messed with the “snap”, so I’ll try that and let you know.

You may have exceeded my limited knowledge in programming with the “inserting-prints-into-your-code” suggestion as I don’t know what that is, but I’ll see if I can figure it out. :slight_smile:

You can have your code print out what it’s doing for anything you like by debugging to a log: http://unity3d.com/support/documentation/ScriptReference/Debug.Log.html

You can do this for pretty much anything - it is useful to find out exactly what is going on with your script when it executes.