- Home /
Question by
Ibbaasilmo · Jul 16, 2021 at 11:25 AM ·
rigidbody2dvector2rigidbody.velocity
How do I make it so the player cant spam the dash
I tried a dash script I found on YouTube. It works fine but I want to know how to make it so that player can't spam the dash. Here's the script I use for my player's dash` public class PlayerDash : MonoBehaviour { private Rigidbody2D rb; public float dashSpeed; private float dashTime; public float startDashTime; public int direction;
// Start is called before the first frame update
void Start()
{
rb = GetComponent<Rigidbody2D>();
dashTime = startDashTime;
}
// Update is called once per frame
void Update()
{
if (direction == 0)
{
if (Input.GetKeyDown(KeyCode.X))
{
if (rb.velocity.x < 0.01f)
{
direction = 1;
}
if (rb.velocity.x > 0.01f)
{
direction = 2;
}
}
}
else
{
if (dashTime <= 0)
{
direction = 0;
dashTime = startDashTime;
rb.velocity = Vector2.zero;
}
else
{
dashTime -= Time.deltaTime;
if (direction == 1)
{
rb.velocity = Vector2.left * dashSpeed;
}
else if (direction == 2)
{
rb.velocity = Vector2.right * dashSpeed;
}
}
}
}
} `
Comment
Answer by IvanBgd · Jul 16, 2021 at 01:20 PM
I know there are better solutions but i would do this:
bool checkTime = true;
public void Dash()
{
if(checkTime == true)
{
//actual Dash
}
checkTime = false;
Invoke("WaitForDash", 2f); //2f is 2 seconds but you can put as much as you want
}
public void WaitForDash()
{
checkTime = true;
}