- Home /
When pressing button for dashing while mid-air, player stops moving for a short time
Hi, I'm currently working on a dash. The way I did it is to just increase the walking space while pressing the button for dash. This leads to a few problems: If I'm not moving and pressing dash, the player can't dash. This leads to another problem: Because I have to set the gravity to 0 while dashing, if I press dash mid-air, the player stops moving for a short time (because the gravity is 'zero'), and then starts falling. Can anyone help me? Here is the whole script for my game: using System.Collections; using System.Collections.Generic; using UnityEngine; public class PlayerMovement : MonoBehaviour { public Rigidbody2D rb; public float moveSpeed; [Header("Dashing")] public bool canDash = true; public float dashingTime; public float dashSpeed; public float dashJumpIncrease; public float timeBtwDashes; // Start is called before the first frame update void Start() { rb = GetComponent<Rigidbody2D>(); sr = GetComponent<SpriteRenderer>(); } // Update is called once per frame void Update() { moveInput = Input.GetAxisRaw("Horizontal"); FaceMoveDirection(); Jump(); if (isGrounded && !Input.GetButton("Jump")) { doubleJump = false; } } void FixedUpdate() { animator.SetFloat("Speed", Mathf.Abs(moveInput)); rb.velocity = new Vector2(moveInput * moveSpeed, rb.velocity.y); canMove = true; if (canMove) { if (Input.GetKey(KeyCode.LeftShift)) { DashAbility(); } } } void FaceMoveDirection() { if (moveInput > 0) { sr.flipX = false; } else if (moveInput < 0) { sr.flipX = true; } } void OnDrawGizmos() { Gizmos.DrawWireSphere(feetPos.position, checkRadius); Gizmos.DrawWireSphere(CeilingCheck.position, checkRadius); } void DashAbility() { if (canDash){ StartCoroutine(Dash()); if (moveInput == 0){ Debug.Log("zero"); } } } IEnumerator Dash() { canDash = false; moveSpeed = dashSpeed; jumpForce = dashJumpIncrease; rb.gravityScale = 0; rb.velocity = Vector2.zero; yield return new WaitForSeconds(dashingTime); moveSpeed = 8f; jumpForce = 9f; rb.gravityScale = 5; yield return new WaitForSeconds(timeBtwDashes); canDash = true; } }
Why does my code look like this? This is my first post, I don't know what happened, apologize