Stopping the player from being able to jump infinitely.

This may be a silly question but I’m completely new to Unity and programming in C# and this is only my 4th day doing so. I’ve got the player movement script down and a few playable levels but I would like to be able to give the player the ability to jump. So I did that and tested it, but at the moment the player can jump infinitely rendering the levels pointless. how would I be able to simply limit the input or the hight the player can reach?
I’ve included the code so let me know if there’s anything else I can improve on or if there are more efficient ways of doing anything.

using UnityEngine;

public Rigidbody rb;

public class PlayerMovement : MonoBehaviour
{
    public float forwardForce    = 2000f;
    public float sidewaysForce  = 500f;
    public float jumpForce        = 100;
   .
    void FixedUpdate()
    {
        rb.AddForce(0, 0, forwardForce * Time.deltaTime);                                                                        
  
        if ( Input.GetKey("d"))
        {
            rb.AddForce(sidewaysForce * Time.deltaTime, 0, 0, ForceMode.VelocityChange);                                   
        }                                                                                                                   
        
     
     
        if (Input.GetKey("a"))
        {
            rb.AddForce(-sidewaysForce * Time.deltaTime, 0, 0, ForceMode.VelocityChange);                               
        }                                                                                                                 


        if (Input.GetKey("space"))
        {
            rb.AddForce(jumpForce * Time.deltaTime, jumpForce, 0, 0);
        }
     
     
        if (rb.position.y < -1f)
        {
            FindObjectOfType<GameManager>().EndGame();                                                                    
        }

     
     
    }

}

Try this:

if (Input.GetKeyDown("space"))
        {
            rb.AddForce(jumpForce * Time.deltaTime, jumpForce, 0, 0);
        }

GetKey works as long as you hold the button, GetKeyDown only in the frame in which to you start pressing it. To prevent player from jumping while already in the air you have to check if it’s grounded

Siudinho’s Approach will get around the Problem that you constantly add force while the space key is pressed. It will not, however, prevent the Player from repeatedly hitting the the space key and similarly start flying around.

If you look at the Problem more closely you will probably find that you want a behaviour similar to this:

  1. The Player should be able to jump
  2. When the Player jumps, the Player should start moving up, and after some time get back to the ground
  3. Only after the Player has touched ground, should they be able to jump again

The last point is crucial. You should keep a boolean variable “isGrounded” that is set when the Player touches ground, and cleared when they jump. Only when isGrounded is true should you allow jumping.

isGrounded is set again when the ground collider tells you that the Player has touched ground again.

1 Like

I actually mentioned this problem :stuck_out_tongue:

Anyways, best way would be to create a method returning boolean, which casts Ray from the player(transform.position) directly below it(Vextor3.down), offsetting it by its height(Collider.bounds.extents.y) + small number(0.1f) to prevent glitching, something like:

Physics.Raycast(transform.position, Vector3.down, Collider.bounds.extents.y + 0.1f);

This is classic example of why FSM was invented in first place. Check this out: