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