Cardinal Direction Dashing Error

I’m designing a platformer where the player can dash in each cardinal direction (up, down, left, right). At the moment the player is able to dash, but not in each direction. The player can only dash vertically (up, down), but not horzontally(left, right). I would be grateful for any guidance.

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class PlayerMovement04 : MonoBehaviour
{
    [Header("Movement Parameter")]
    [SerializeField] private float speed; // make player speed an object
    [SerializeField] private float jumpPower; // make player jump an object
    [SerializeField] private float gameGravity; // ingame gravity

    [Header("Dash")]
    [SerializeField] private float dashDistance; // distance that is traveled over dash
    private bool isDashing; // if player is currently dashing
    private bool dashCounter; // if player is able to dash

    [Header("Wall Jumps")]
    [SerializeField] private float wallJumpHeight; //veritcal distance
    [SerializeField] private float wallJumpDistance; // horizontal distance

    [Header("Multipe Jumps")]
    [SerializeField] private int extraJumps; // how many extra jumps the player gets
    private int jumpCounter; // how many times player has jumped

    [Header("Layers")]
    [SerializeField] private LayerMask groundLayer; // make groundLayer an object
    [SerializeField] private LayerMask wallLayer; // make wallLayer an object

    private Rigidbody2D body; // make player as an object
    private Animator anim; // make animationa na object
    private BoxCollider2D boxCollider; // make boxcollider an object
    private float horizontalInput; // detects whether player is moving left or right

    private void Awake() { // when game begins
        body = GetComponent<Rigidbody2D>(); // gain player as an object
        anim = GetComponent<Animator>(); //gain animation as an object
        boxCollider = GetComponent<BoxCollider2D>();
    }

    private void Update() { // check everyframe when game is active

        horizontalInput = Input.GetAxis("Horizontal"); //checks current horizontal movement

        if (horizontalInput > 0.01f) { //flips character image when turning left or right
            transform.localScale = Vector3.one;
        } else if (horizontalInput < -0.01f) {
            transform.localScale = new Vector3(-1, 1, 1);
        }

        //set animator parameters
        anim.SetBool("Run", horizontalInput != 0);
        anim.SetBool("Grounded", isGrounded());

        //Jump
        if (Input.GetKeyDown(KeyCode.Space)) {
            jump();
        }

        //Dash
        if (Input.GetKeyDown(KeyCode.E)) {
            dash();
        }

        //Adjustable jump height
        if (Input.GetKeyUp(KeyCode.Space) && body.velocity.y > 0) {
            body.velocity = new Vector2(body.velocity.x, body.velocity.y / 2);
        }

        //
        if (onWall()) { // allows player to stick to wall
            body.gravityScale = 0;
            body.velocity = Vector2.zero;
        }
        else {
            body.gravityScale = gameGravity;
            if (!isDashing) { // if player it not dashing allow them to move left and right
                body.velocity = new Vector2(horizontalInput * speed, body.velocity.y);
            }

            if (isGrounded()) { // resets both double jump & dash
                jumpCounter = extraJumps; // resets counter once player touches ground
                dashCounter = true;
            }
        }
       
    }

    private void dash() {
        if (!dashCounter) return; //if player is not meant to dash

        // if player is currently holding "A" down
        if (Input.GetKey(KeyCode.A)) { // left
            isDashing = true;
            body.velocity = new Vector2(-dashDistance, 0f);
            dashCounter = false;
            isDashing = false;
        }
        // if player is currently holding "D" down
        else if (Input.GetKey(KeyCode.D)) { // right
            isDashing = true;
            body.velocity = new Vector2(dashDistance, 0f);
            dashCounter = false;
            isDashing = false;
        }
        // if player is currently holding "W" down
        else if (Input.GetKey(KeyCode.W)) { // up
            isDashing = true;
            body.velocity = new Vector2(0f, dashDistance);
            dashCounter = false;
            isDashing = false;
        }
        // if player is currently holding "S" down
        else if (Input.GetKey(KeyCode.S)) { // down
            isDashing = true;
            body.velocity = new Vector2(0f, -dashDistance);
            dashCounter = false;
            isDashing = false;
        }
    }

    private void jump(){ // jumping method

        //SoundManager.instance.PlaySound(jumpSound);

        if (jumpCounter <= 0) return; //if player is not meant to jump

        if (onWall()) {
            wallJump();
        }
        else {
            if(isGrounded()) { // normal jump
                body.velocity = new Vector2(body.velocity.x, jumpPower);
            }
            else {
                if (jumpCounter > 0) { // if player can double jump let them
                    body.velocity = new Vector2(body.velocity.x, jumpPower);
                    jumpCounter--;
                }
            }
        }

    }

    private void wallJump() { // wall jump
        body.AddForce(new Vector2(-Mathf.Sign(transform.localScale.x) * wallJumpDistance, wallJumpHeight));
        //wallJumpCooldown = 0;
    }

    private bool isGrounded(){ // check if player is grouned or not
        RaycastHit2D raycastHit = Physics2D.BoxCast(boxCollider.bounds.center, boxCollider.bounds.size, 0, Vector2.down, 0.1f, groundLayer);
        return raycastHit.collider != null;
    }

    private bool onWall(){ // check if player is connected to wall or not
        RaycastHit2D raycastHit = Physics2D.BoxCast(boxCollider.bounds.center, boxCollider.bounds.size, 0, new Vector2(transform.localScale.x, 0), 0.1f, wallLayer);
        return raycastHit.collider != null;
    }
}

Let me be straight here: your code is very confusing. It took me a while to even find your normal movement code. Who would expect it to be in the else statement of not wall running? You use a variable names dashCounter, implying it’s a number, but is actually a bool.

Anyways, your problem itself is quite easy to explain. You always overwrite your hoizontal velocity directly in your movement code. Line 77 in the posted code example. Velocity is applied over multiple frames. Your dash code sets it to something and then your normal movement code sets it to something else in the very same frame.
You seem to have a misconception about how code is executed, since your isDashing bool does absolutely nothing. You set it to true, adjust the velocity and set it to false. Nothing happened inbetween, except overwriting the velocity. No frames were executed in which the character could have moved. The only reason this works vertically, is since line 77 does not touch the vertical velocity.

Hope this helps you figure out what to do.
You might want to watch a youtube series on platformer movement or dashing first.

1 Like

Thank you. This makes sense and helps to clear things up.

1 Like