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.