Hello, I am new to unity and to coding as a whole.
Right now I have a dash and roll mechanic
Here is how its triggered
if (Input.GetKeyDown(KeyCode.LeftShift) && IsGrounded() && canDash)
{
StartCoroutine(Roll());
}
else if (Input.GetKeyDown(KeyCode.LeftShift) && canDash)
{
StartCoroutine(AirDash());
}
here is coroutine roll (which I have no problems with)
private IEnumerator Roll()
{
canDash = false;
isDashing = true;
if (IsGrounded())
{
isRolling = true;
}
rb.gravityScale = 0f;
if (facingRight)
{
rb.velocity = new Vector2(1 * dashingPower, 0f);
}
else
{
rb.velocity = new Vector2(-1 * dashingPower, 0f);
}
yield return new WaitForSeconds(dashingTime);
rb.gravityScale = originalGravity;
isDashing = false;
if (isRolling)
{
isRolling = false;
}
yield return new WaitForSeconds(dashingCooldown);
canDash = true;
}
here is coroutine AirDash
private IEnumerator AirDash()
{
canDash = false;
isDashing = true;
rb.gravityScale = 0f;
if (facingRight)
{
rb.velocity = new Vector2(1 * dashingPower, 0f);
}
else
{
rb.velocity = new Vector2(-1 * dashingPower, 0f);
}
if (rb.transform.localScale.x < 1.5 && rb.transform.localScale.y < 1.5 && rb.transform.localScale.x > 0.5 && rb.transform.localScale.y > 0.5)
{
rb.transform.localScale -= new Vector3(0.01f, 0.01f, 0f);
}
yield return new WaitForSeconds(dashingTime);
rb.gravityScale = originalGravity;
isDashing = false;
}
Now the issue is when I can’t figure out how to get canDash to true when the player hits the ground so that they cant dash multiple times in air.
I have an IsGrounded bool…
private bool IsGrounded()
{
return Physics2D.BoxCast(coll.bounds.center, coll.bounds.size, 0f, Vector2.down, .1f, jumpableGround);
}
but when I put at the end of the AirDash something like
if(IsGrounded())
{
canDash = true;
}
it just doesnt work.
The only other thing i could think of was to put in update ifgrounded candash is true but then there is no dash cooldown on the ground.
hopefully this isnt too confusing. if not please help. thank you!!!