How do I stop infinite jumping?

So, i am very new to unity and just started my new game today. Everything is right, except from jumping. I can jump infinitely in mid-air and i dont want that.

Tha script :

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class PlayerMovement : MonoBehaviour {

    public CharacterController2D controller;

    public float runSpeed = 40f;
   
    float horizontalMove = 0f;
    bool jump = false;

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

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

        if (Input.GetButtonDown("Jump"))
        {
            jump = true;
        }

    }
   
     void FixedUpdate ()
        {

        controller.Move(horizontalMove * Time.fixedDeltaTime, false, jump);
        jump = false;
        }   
        }

Hi @Illuminati23 ,
You don’t have any rule in your jump button press - You do record the information that you are jumping, but you don’t test for it anywhere. So, why not test like this:

if (Input.GetButtonDown("Jump") && jump != true)

Then, only when your character lands / dies (or whatever cases you might have) you reset the jump value.

1 Like

Where your Input.GetButtonDown(“Jump”) is? :slight_smile:

It’s logical AND operator. (Boolean logical operators - the boolean and, or, not, and xor operators - C# reference | Microsoft Learn). For sure it will work, you must have some typo or something else wrong. && means that you need both of those rules to match in order for code to proceed into that block.

if (Input.GetButtonDown("Jump") && jump != true)
{
    jump = true;
}
1 Like

OK so i did what you said but i can still infinte jump HELP
EDIT : and it says there are no issues found

How do you actually make the jump happen? Do you add force to a Rigidbody which is on your character, or add some value on every update?

It might be a good idea to watch a few tutorials on basics of character controllers, there are many good ones out there.

I did, I watched the “player movement 2d tutorial” by brackeys. I wish I could change my code but when I go to other videos the whole script is different.

Put Your GroundCheck and CeilingCheck in the player object