Dash Movement script works until if statement is added

This script works until the section of code I marked with a comment is added and i can’t understand why… I’m thinking about reworking my basic movement controls to something more written out that i know will work but i’d like to have this more simple and graceful code more than something basic. If anyone can tell me what I’m doing wrong that’d be awesome!

public class Player : MonoBehaviour
{

    public float speed = 5f;
    public Rigidbody2D rb;
    [SerializeField] 
    private float dashMultiplier = 1f;
    private Vector3 tempVect;
    
    void Update()
    {
        /* if (Input.GetButton("Space"))
        {
            dashMultiplier = 1.3f;
        } */

        DashCheck(dashMultiplier);

        float h = Input.GetAxis("Horizontal");
        float v = Input.GetAxis("Vertical");

        tempVect = new Vector2(h, v);
        tempVect = tempVect.normalized * speed * Time.deltaTime;
        rb.MovePosition(rb.transform.position + tempVect);

        if (Input.GetButtonUp("Up"))
        {
            rb.velocity = Vector3.zero;
        }

        else if (Input.GetButtonUp("Down"))
        {
            rb.velocity = Vector3.zero;
        }

        else if (Input.GetButtonUp("Left"))
        {
            rb.velocity = Vector3.zero;
        }

        else if (Input.GetButtonUp("Right"))
        {
            rb.velocity = Vector3.zero;
        }

        speed = 5f;
        dashMultiplier = 1f;
    }

    void DashCheck (float dashMultiplier)
    {
        speed = speed * dashMultiplier;
    }
}

This does not work because you are using GetButton to reference your Space key. GetButton returns true only if the virtual button with the given name is being pressed, and those buttons are defined at the InputManager in ProjectSettings. By default there is no virtual button with the name “Space”, if you have created one with this name then the problem is something else, however, I think that if you change your condition inside the if to

if (Input.GetKeyDown(KeyCode.Space))

it should compile correctly, but I still think your dash would be weird.


I recommended GetKeyDown since most dashes in games are done by just pressing the button once, but it seems like your code would only work if the player press and hold the space key (which a GetKey would help)… But in this way the player could use a dash all the time, if I understood it correctly.