I’m making a dash function so when I click the button 2 times the player dashes (like in some other 2d games) and I want to implement a timer so when I click the first time if I don’t click the second time fast enough it resets the dash.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Movement : MonoBehaviour
{
private Rigidbody2D rigidbody2d;
float moveSpeed = 5;
float dashSpeed = 12;
float dashSpeedUp = 5;
float dashCount = 0f;
void Start()
{
rigidbody2d = transform.GetComponent<Rigidbody2D>();
}
void Update()
{
Dash();
}
private void Dash()
{
if (Input.GetKeyDown(KeyCode.A))
{
if (dashCount >= 0)
{
dashCount = 0;
print(dashCount);
}
dashCount -= 1;
print(dashCount);
if (dashCount == -2)
{
rigidbody2d.velocity = new Vector3(-dashSpeed, +dashSpeedUp);
dashCount = 0;
print(dashCount);
}
}
if (Input.GetKeyDown(KeyCode.D))
{
if (dashCount <= 0)
{
dashCount = 0;
print(dashCount);
}
dashCount += 1;
print(dashCount);
if (dashCount == 2)
{
rigidbody2d.velocity = new Vector3(+dashSpeed, +dashSpeedUp);
dashCount = 0;
print(dashCount);
}
}
}
}