I am trying to make my character dash, and as soon as i add a simple cooldown to the dash, the dash doesnt complete, it only does about half the dash, i have tried a few things, but the results are always the same, i really need help with this
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlayerMove : MonoBehaviour
{
public float speed;
public float jumpforce;
private float moveInput;
private Rigidbody2D rb;
private bool facingRight = true;
private bool isGrounded;
public Transform feetPos;
public float checkRadius;
public LayerMask whatIsGround;
private float jumpTimeCounter;
public float jumpTime;
private bool isJumping;
public float dashSpeed;
private float dashTime;
public float startDashTime;
private int direction;
public float cooldownTime = 2;
private float nextDashTime = 0;
private bool WaitForDash = false;
IEnumerator WaitDash()
{
yield return new WaitForSeconds(2);
}
void Start()
{
rb = GetComponent<Rigidbody2D>();
Vector3 Scaler = transform.localScale;
Scaler.x *= -1;
transform.localScale = Scaler;
}
void FixedUpdate()
{
moveInput = Input.GetAxisRaw("Horizontal");
rb.velocity = new Vector2(moveInput * speed, rb.velocity.y);
if (facingRight == false && moveInput > 0)
{
Flip();
}
else if (facingRight == true && moveInput < 0)
{
Flip();
}
if (direction == 0)
{
if (Input.GetKeyDown(KeyCode.LeftShift))
{
if (Input.GetKey(KeyCode.W))
{
direction = 3;
}
}
if (Input.GetKeyDown(KeyCode.LeftShift))
{
if (Input.GetKey(KeyCode.D))
{
direction = 2;
}
}
if (Input.GetKeyDown(KeyCode.LeftShift))
{
if (Input.GetKey(KeyCode.A))
{
direction = 1;
}
}
}
else
{
if (Time.time > nextDashTime)
{
if (dashTime <= 0)
{
direction = 0;
dashTime = startDashTime;
rb.velocity = Vector2.zero;
}
else
{
dashTime -= Time.deltaTime;
if (direction == 1)
{
rb.velocity = Vector2.left * dashSpeed;
nextDashTime = Time.time + cooldownTime;
}
else if (direction == 2)
{
rb.velocity = Vector2.right * dashSpeed;
nextDashTime = Time.time + cooldownTime;
}
else if (direction == 3)
{
rb.velocity = Vector2.up * dashSpeed;
nextDashTime = Time.time + cooldownTime;
}
}
}
}
}
void Flip()
{
facingRight = !facingRight;
Vector3 Scaler = transform.localScale;
Scaler.x *= -1;
transform.localScale = Scaler;
}
void Update()
{
isGrounded = Physics2D.OverlapCircle(feetPos.position, checkRadius, whatIsGround);
if (isGrounded == true && Input.GetKeyDown(KeyCode.Space))
{
isJumping = true;
jumpTimeCounter = jumpTime;
rb.velocity = Vector2.up * jumpforce;
}
if (Input.GetKey(KeyCode.Space) && isJumping == true)
{
if (jumpTimeCounter > 0)
{
rb.velocity = Vector2.up * jumpforce;
jumpTimeCounter -= Time.deltaTime;
}
else
{
isJumping = false;
}
if (Input.GetKeyUp(KeyCode.Space))
{
isJumping = false;
}
}
}
}