How do I make my character move midair?

Can anyone help me make my character move mid air while it’s jumping? I don’t know how to fix this and also sometimes it randomly infinitely jumps.

using UnityEngine;

public class PlayerMovement : MonoBehaviour {

public CharacterController2D controller;
public Animator animator;

public float runSpeed = 80f;

float horizontalMove = 0f;
bool jump = false;
bool crouch = false;

// Update is called once per frame
void Update() {

    horizontalMove = Input.GetAxisRaw("Horizontal") * runSpeed;

    animator.SetFloat("Speed", Mathf.Abs(horizontalMove));

    if (Input.GetButtonDown("Jump"))
    {
        jump = true;
        animator.SetBool("IsJumping", true);
    }

    if (Input.GetButtonDown("Crouch"))
    {
        crouch = true;
    } else if (Input.GetButtonUp("Crouch"))
    {
        crouch = false;
    }

}

 public void OnLanding ()
 {
    animator.SetBool("IsJumping", false);
 }

public void OnCrouching (bool isCrouching)
{
    animator.SetBool("IsCrouching", isCrouching);
}

void FixedUpdate ()
{
    // Move our character
    controller.Move(horizontalMove * Time.fixedDeltaTime, crouch, jump);
    jump = false;
}

}
,I don’t know how to make my character move in midair and the tutorial I was watching didn’t help me. Here’s the code. Can anyone help me?

using UnityEngine;

public class PlayerMovement : MonoBehaviour {

public CharacterController2D controller;
public Animator animator;

public float runSpeed = 80f;

float horizontalMove = 0f;
bool jump = false;
bool crouch = false;

// Update is called once per frame
void Update() {

    horizontalMove = Input.GetAxisRaw("Horizontal") * runSpeed;

    animator.SetFloat("Speed", Mathf.Abs(horizontalMove));

    if (Input.GetButtonDown("Jump"))
    {
        jump = true;
        animator.SetBool("IsJumping", true);
    }

    if (Input.GetButtonDown("Crouch"))
    {
        crouch = true;
    } else if (Input.GetButtonUp("Crouch"))
    {
        crouch = false;
    }

}

 public void OnLanding ()
 {
    animator.SetBool("IsJumping", false);
 }

public void OnCrouching (bool isCrouching)
{
    animator.SetBool("IsCrouching", isCrouching);
}

void FixedUpdate ()
{
    // Move our character
    controller.Move(horizontalMove * Time.fixedDeltaTime, crouch, jump);
    jump = false;
}

}

@ninjaweirdoyt What you probably want to do is clean up all those logic checks by implementing a state machine. The following page not only covers state machines but essentially implements the exact example that you are looking for: State · Design Patterns Revisited · Game Programming Patterns . I’d highly recommend reading the whole thing and checking out more pages from that website in general.


What is most likely happening in your code is that you have horizontal movement and jumping completely separated by logic checks as being mutually exclusive - if you are jumping, you cannot move, by the specific way you’ve written your code. I’d suggest taking a step back from the syntax to look at the big picture of your coding process: what will happen when you add more features, like swimming, for example? When you jump, you’ll have to check you’re not already jumping, that you’re not crouching, that you’re not swimming - when you swim, you’ll have to check you’re crouching or jumping. If you add more character features these logic gates are just going to get worse and spiral out of control incredibly quickly. A state machine will fix this up pretty well.