Cannot move while in the air?

On my player controller I have a coroutine designed to make the player jump

public IEnumerator Jump() {
        isJumping = true;
        GetComponent<Rigidbody2D>().velocity = new Vector2(moveVelocity, jumpHeight);
        playerAnim.SetInteger("currentState", 4);
        yield return new WaitForSeconds(1f);
        if(Input.GetKey(KeyCode.A) || Input.GetKey(KeyCode.D)) {
            playerAnim.SetInteger("currentState", 3);
        } else {
            playerAnim.SetInteger("currentState", 2);
        }
        isJumping = false;
    }

but this is only called if the jump key is pressed. In the Update loop I handle movement like this

     if (Input.GetKeyDown (KeyCode.W) && !isJumping && canMove && !onLadder) {
       StartCoroutine("Jump");
     }
     moveVelocity = 0f;

     if (Input.GetKey (KeyCode.D) && canMove) {
       moveVelocity = moveSpeed;
       if(isJumping || isRolling) {
         return;
       }
       playerAnim.SetInteger("currentState", 3);
     }
     if (Input.GetKey (KeyCode.A) && canMove) {
       moveVelocity = -moveSpeed;
       if(isJumping || isRolling) {
         return;
       }
       playerAnim.SetInteger("currentState", 3);
     }
     if (moveVelocity == 0 && canMove && !isJumping && !isRolling) {
       playerAnim.SetInteger("currentState", 2);
     }

     GetComponent<Rigidbody2D>().velocity = new Vector2(moveVelocity, GetComponent<Rigidbody2D>().velocity.y);

Whenever I am in the air however, I cannot move. I’ve tried a few things, but I can’t seem to get it to work. I don’t understand the conflict that is preventing me from moving mid-air. If someone can see it let me know, thanks!

EDIT: Whenever I’m in the air and a directional key is pressed, the moveVelocity float does update, but I still do not move. Just thought you should know

if (isJumping || ...)
    return;

The coroutine sets isJumping to true when it begins, then sets the field to false when it exits. Your update method returns when isJumping is true, prior to setting the velocity of the rigidbody.

1 Like

You set isJumping to true in the coroutine which causes all your input checks to early out without setting a velocity.

1 Like

Ah I see, I was thinking that the velocity was set regardless of the conditional statement, and the conditional only stopped my animation state from changing. Thanks for clearing this up :slight_smile: