Okay, so I am trying to implement a 5 second cooldown feature into my 2d game. However, after i added the cooldown feature the dash length starts to decrease the less amount of time i tap the button. Could someone help me fix this? I’m a noob gamedev so please try to keep it simple.
Thanks!
Here is my code
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Dash : MonoBehaviour
{
public PlayerMovement playerMovement;
public CharacterController2D controller;
public float dashSpeed;
private float dashTime;
public float startDashTime;
private int direction;
private bool ableToDash;
public float dashCooldown;
void Start()
{
dashTime = startDashTime;
ableToDash = true;
}
public void Update()
{
if (ableToDash == false)
{
dashCooldown -= Time.deltaTime;
if(dashCooldown < 0)
{
ableToDash = true;
dashCooldown = 5f;
}
}
if (direction == 0 && ableToDash)
{
if (Input.GetKeyDown(KeyCode.C))
{
direction = 1;
}
else if (Input.GetKeyDown(KeyCode.V))
{
direction = 2;
}
}
}
// Update is called once per frame
public void FixedUpdate()
{
if (dashTime <= 0)
{
direction = 0;
dashTime = startDashTime;
playerMovement.dashMove = 0f;
}
else
{
dashTime -= Time.deltaTime;
if (direction == 1)
{
playerMovement.dashMove = -1f * dashSpeed;
StartCoroutine(Wait());
ableToDash = false;
}
else if (direction == 2)
{
playerMovement.dashMove = 1f * dashSpeed;
StartCoroutine(Wait());
ableToDash = false;
}
}
}
private IEnumerator Wait()
{
yield return new WaitForSeconds(0.1f);
}
}