How do I stop my character from flying around when I click the space key?

So I started Unity yesterday and I decided to create a character that moves and jumps, walking and jumping its done, although when I press the space the key I can press it multiple times and the character just flies away. So basically I want to make the jump limited, I tried various methods watch various videos but I cant understand how I can solve it, so the forum seems like the best place to show my code:

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

public class scr: MonoBehaviour
{
    public float speed;
    private Rigidbody rb;
    public float jumpforce = 1f;
    public bool isGrounded;
  

    //Vai obter o rigid body do character

    void Start()
    {

        rb = GetComponent<Rigidbody>();
    }

    //Movimentaçao do character
    void Update()
    {
        float moveHorizontal = Input.GetAxis("Horizontal");
        float moveVertical = Input.GetAxis("Vertical");
        float moveUp = Input.GetAxis("Jump");
        Vector3 movement = new Vector3(moveHorizontal, moveUp, moveVertical);

        rb.AddForce(movement * speed);

        if (isGrounded == true && Input.GetKeyDown(KeyCode.Space))
        {
            rb.AddForce(Vector3.up * jumpforce, ForceMode.Impulse);

        }
        if (isGrounded == false && Input.GetKeyDown(KeyCode.Space))
        {
            rb.AddForce(Vector3.down * jumpforce, ForceMode.Impulse);
        }
    }
   
   
}

You’re applying moveUp force everytime you press the Jump button.

float moveUp = Input.GetAxis("Jump");
        Vector3 movement = new Vector3(moveHorizontal, moveUp, moveVertical);
  1. Make sure gravity is on on your rigidbody
  2. You are checking if your character isGrounded, but you are not setting it anywhere. You call need to look into RayCasting for that
  3. Remove the second if statement, it will not be needed once you have set and checked the isGrounded boolean.

Thanks for the help, although as I said Im extremely new and RayCasting is kinda confusing for me at this stage and idk how to properly apply it.

There are plenty of sollutions.
The easiest one (no so much code and thinking) is by creating a sphere collider into the feet of the player, and check the spherecollider isTrigger to be on.
in code just use:

private void OnTriggerEnter(Collider other)
    {
       isGrounded = true;
    }

private void OnTriggerExit(Collider other)
    {
       isGrounded = false;
    }

and use this code

void Update()
    {
        float moveHorizontal = Input.GetAxis("Horizontal");
        float moveVertical = Input.GetAxis("Vertical");
        //float moveUp = Input.GetAxis("Jump");
        Vector3 movement = new Vector3(moveHorizontal, 0, moveVertical);
        rb.AddForce(movement * speed);
        if (isGrounded == true && Input.GetKeyDown(KeyCode.Space))
        {
            rb.AddForce(Vector3.up * jumpforce, ForceMode.Impulse);
        }
        if (isGrounded == false && Input.GetKeyDown(KeyCode.Space))
        {
            rb.AddForce(Vector3.down * jumpforce, ForceMode.Impulse);
        }
    }

The main problem remains isGrounded… i show you a simple solution in code but not elegant :slight_smile: