Home > Enterprise >  Don't understand why my dash doesn't work in Unity2D?
Don't understand why my dash doesn't work in Unity2D?

Time:11-11

I'm creating a 2D platformer on unity, I'm a beginner. My character can go to left and right, and jump (no double jump for the moment). I want to make him dash when I press left shift the arrow key of the direction he goes. I followed several tutos on youtube and chose one inspired by HollowKnight and Celeste.

The problem is when I try to dash while running, it doesn't work, the character just fly/float away instead of just dashing (even if i don't press the jump touch).

Hope my post is enough understable, I'm french so let me know if you need some precisions. I'm pretty sure I have some lines that had nothing to do in the code, but as I don't understand all I read, i try severals things and so my code looks like Frankenstein... Please, be kind

This is my code :

public class DashHollowKnight : MonoBehaviour
{
    [Header("Dashing")]
    [SerializeField]
    private float dashingVelocity = 14f;

    [SerializeField]
    private float dashingTime = 0.5f;

    private Vector2 dashingDir;
    private bool isDashing;
    private bool canDash = true;

    public float speed;
    public float moveInput;

    private bool isOnGround = true;
    public Animator animator;

    public Rigidbody2D playerRb;


    // Start is called before the first frame update
    void Start()
    {
        
    }

    private void OnCollisionEnter2D(Collision2D col)
    {
        isOnGround = true;
    }

    // Update is called once per frame
    void Update()
    {
        var dashInput = Input.GetButtonDown("Dash");

        if (dashInput && canDash)
        {
            isDashing = true;
            canDash = false;
            dashingDir = new Vector2(Input.GetAxisRaw("Horizontal"), Input.GetAxisRaw("Vertical"));
            if (dashingDir == Vector2.zero)
            {
                dashingDir = new Vector2(transform.localScale.x, 0);
            }
            StartCoroutine(StopDashing());

        }

        animator.SetBool("IsDashing", isDashing);

        if (isDashing)
        {
            playerRb.velocity = dashingDir.normalized * dashingVelocity;
            if (dashingTime <= 0)
                playerRb.velocity = new Vector2(moveInput * speed, playerRb.velocity.y);
            return;
        }

        
    }

    private IEnumerator StopDashing()
    {
        yield return new WaitForSeconds(dashingTime);
        isDashing = false;
        canDash = true;
    }
}

I followed several tutos on youtube, the one I chose lokks like the best for what I need.

I follow exactly what the video says, and searched what could make it don't work but I'm stuck.

CodePudding user response:

Try changing the playerRb.velocity axis:

if (isDashing)
{
    playerRb.velocity = dashingDir.normalized * dashingVelocity;
    if (dashingTime <= 0)
        // you have - velocity is added to the y axis (vertical)
        playerRb.velocity = new Vector2(moveInput * speed, playerRb.velocity.y);

        // change to - velocity is added to the x axis (horizontal)
        playerRb.velocity = new Vector2(moveInput * speed, playerRb.velocity.x);
     return;
}

CodePudding user response:

Thanks @ProdigalTechie I changed y axis by x axis but I still have the same result. I continue to search the explication :)

  • Related