I’m making a 2d platformer, I would like to know how to have control in mid-air.
public function Update () : void {
//If our character isn’t jumping
if (!b_isJumping) {
if (Input.GetButton("Horizontal")) {
//Walking
in_direction = Input.GetAxis("Horizontal") < 0 ? -1 : 1;
rigidbody.velocity = new Vector3((in_direction*f_speed), rigidbody.velocity.y, 0);
//Reset Stay animation frame back to the first frame
loopSprites[0].ResetFrame();
//Update Walking animation while the character is walking
loopSprites[1].UpdateAnimation(in_direction, renderer.material);
} else {
//Stay
//Reset Walking animation frame back to the first frame
loopSprites[1].ResetFrame();
//Update Stay animation while the character is not walking
loopSprites[0].UpdateAnimation(in_direction, renderer.material);
}
//Jump
if (Input.GetButton("Jump")) {
b_isJumping = true;
//Then make it Jump
audio.volume = 0.3;
audio.PlayOneShot(jumpSound);
loopSprites[0].ResetFrame();
loopSprites[1].ResetFrame();
rigidbody.velocity = new Vector3(rigidbody.velocity.x, -Physics.gravity.y, 0);
}
} else {
//update animation while it Jump
jumpSprite.UpdateJumpAnimation(in_direction, rigidbody.velocity.y, renderer.material);
}
}