(C# and 2D) Jump and Collision help

I’ve recently started trying to learn Unity again and so far I’ve gotten some basic movements done including a jump, however I can just keep pressing the jump button to go higher and higher, I don’t want to be able to jump unless the player object is touching the ground object, I’ve done a lot of googling but I couldn’t figure anything out. Heres the code i’m written so far

    Rigidbody2D rb;

    // Use this for initialization
    void Start () {
        rb = GetComponent<Rigidbody2D>();
     
    }

    public float moveSpeed = 2.0f;
    public float jumpHeight = 200.0f;


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

        var left = Input.GetKey(KeyCode.A);
        var right = Input.GetKey(KeyCode.D);
        var jump = Input.GetKeyDown(KeyCode.Space);

        if (left) {
            transform.Translate(Vector2.left * moveSpeed * Time.deltaTime);
        }
        if (right)
        {
            transform.Translate(Vector2.right * moveSpeed * Time.deltaTime);
        }
        if (jump) {
            rb.AddForce(transform.up * jumpHeight, ForceMode2D.Force);
        }
    }

You’ve got to know if you’re on the ground. You can raycast down, for example.
Other options might be collision/trigger enter or overlapsphere checks (on ground layers, to keep it cheaper).

Recorded Video Session: 2D Platformer Character Controller - Unity Learn Maybe look at Unity’s 2D tutorials. Here is one with jump in it. I think they have a few others, but not sure what way they are detecting being on the ground.

can you give me an example of one of those, I cant figure them out

There are at least 1 or 2 standard asset controllers, too, which you could look at :slight_smile:

Please try @Brathnann 's suggestion, and/or the standard assets to get an idea and see what you can do from there.