2D platformer character can't dash while running

Hi, guys, I was watching a 2D platformer movement tutorial and creating a dash for my character, but I found the character can’t dash while running or jumping. I can dash only I didn’t input any directional key, which means I can only dash when I only press E or press E after I jump in place. Why this happened?

If you are willing to reply my question, I will be really appreciate it.(and I am a starter for unity, so this script must have so many problems but please point out the problems only relate to this thread)

Here are the scripts:

{
rb = GetComponent<Rigidbody2D>();
animator = GetComponent<Animator>();
}
// Update is called once per frame
void Update()
{
float horizontal = Input.GetAxis("Horizontal");
float faceDirection = Input.GetAxisRaw("Horizontal");
Vector2 nowPosition = new Vector2(horizontal, rb.velocity.y);
Run(nowPosition, faceDirection);
onGround = Physics2D.OverlapCircle(groundCheck.position, collisionRadius, groundLayer);
canDash = Time.time > nextDashTime;
Jump();
animator.SetFloat("verticalY", rb.velocity.y);
animator.SetBool("OnGround", onGround);
animator.SetBool("canDash", canDash);
Dash();
}
void Run(Vector2 v2,float faceDirection)
{
if (v2.x!=0)
{
float newSpeed = v2.x * runSpeed;
rb.velocity = new Vector2(newSpeed, rb.velocity.y);
}
if (faceDirection !=0)
{
transform.localScale = new Vector3(faceDirection, 1, 1);
}
animator.SetFloat("running", Mathf.Abs(faceDirection));

}
void Jump()
{
if (Input.GetButtonDown("Jump") && onGround)
{
rb.velocity = new Vector2(rb.velocity.x, 0);
rb.velocity += Vector2.up * jumpForce;
if (rb.velocity.y>0)
{
animator.SetTrigger("jump");
CreateJumpEffect();
}
//animator.SetBool("OnGround", false);
}
}

void Dash()
{
if (canDash)
{
if (Input.GetKeyDown(KeyCode.E))
{
rb.velocity = new Vector2(transform.localScale.x * dashSpeed, 0f);
nextDashTime = Time.time + dashCoolDownTime;
if (rb.velocity.x>0)
{
animator.SetTrigger("dash");
Debug.Log("Working");
}
}
}

}
void CreateJumpEffect()
{
jumpEffect.Play();
}
}```

Hey there,
Please use code tags to post some code, as it is currently unreadable :wink:

Thank you for your remind. I am new here, still learning the rules.

Your code format still looks kinda off, but it’s ok :slight_smile:

Since it looks like you are still at an early stage of that project my advice would be to move on to the official InputSystem Package, available in the Package Manager window. There are already a lot of good tutorials on this system, and it is much more efficient than using GetKey and GetKeyDown.

Also, I assume you are relatively new to programming, which is absolutely fine of course, but keep in mind that clean code is not a luxury, it is a requirement. Your code currently has a lot of “if/else” statements, that cause a high level of indentation and decrease readbility, but also make it way more fallible. Using the InputSystem Package will help you reduce these statements and have cleaner code, but this also applies to ALL your project : do not ONLY learn about programming in Unity : learn about programming with GOOD PRACTICES.

Read about properties/fields, public/private/protected/internal, read about abstract/virtual, about getter and setters, return values, and so on ! This will be absolutely mandatory for you at some point, so save yourself some time and some code cleaning down the road :slight_smile:

Let me show you another, cleaner, way of writing your Dash function. It’s not even the best you could do, as it is not changing much in it, but it is already much more readable and debuggable :

    private void Dash()
    {
        // the exclamation point means "if not"
        if (!canDash)
            return; //no need to curly brackets because you only have one statement after "if"
                    //return means that since the player can not dash, the function needs to stop here and return to the method that called it

        if (Input.GetKeyDown(KeyCode.E))
        {
            rb.velocity = new Vector2(transform.localScale.x * dashSpeed, 0f);

            nextDashTime = Time.time + dashCoolDownTime;

            //no need to check for rb.velocity since you already set it a few line earlier
            animator.SetTrigger("dash");
        }
    }

Thank you so much for your advices. I didn’t know they have a new input systems, I will try to use it. Thank you, I really appreciate it.