Search Unity

Stop Horizontal Movement Inputs while Dashing Down (2D)

Discussion in 'Scripting' started by LotusZiggs, Sep 23, 2022.

  1. LotusZiggs

    LotusZiggs

    Joined:
    Aug 25, 2022
    Posts:
    4
    I recently implemented a simple code for dashing downwards while in the air. The issue I'm having is that I want to stop the player from being able to move left/right while the dash is taking place. I've tried setting the horizontal value to 0 but this doesn't seem to overwrite the current horizontal movement code. Here are some lines from the code I think have to do with the issue:

    Code (CSharp):
    1.         // If player is not grounded, then dash downwards
    2.         if (!IsGrounded() && Input.GetKeyDown(KeyCode.Q))
    3.         {
    4.             rb.velocity = new Vector2(0f, transform.localScale.y * -downPower);
    5.         }
    Here is my horizontal movement code:

    Code (CSharp):
    1. horizontal = Input.GetAxisRaw("Horizontal");
    Any advice would help. Thank you!