Search Unity

Stopping the player from being able to jump infinitely.

Discussion in 'Scripting' started by Deleted User, Apr 21, 2019.

  1. Deleted User

    Deleted User

    Guest

    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.

    Code (CSharp):
    1.  
    2. using UnityEngine;
    3.  
    4. public Rigidbody rb;
    5.  
    6. public class PlayerMovement : MonoBehaviour
    7. {
    8.     public float forwardForce    = 2000f;
    9.     public float sidewaysForce  = 500f;
    10.     public float jumpForce        = 100;
    11.    .
    12.     void FixedUpdate()
    13.     {
    14.         rb.AddForce(0, 0, forwardForce * Time.deltaTime);                                                                        
    15.  
    16.         if ( Input.GetKey("d"))
    17.         {
    18.             rb.AddForce(sidewaysForce * Time.deltaTime, 0, 0, ForceMode.VelocityChange);                                  
    19.         }                                                                                                                  
    20.        
    21.      
    22.      
    23.         if (Input.GetKey("a"))
    24.         {
    25.             rb.AddForce(-sidewaysForce * Time.deltaTime, 0, 0, ForceMode.VelocityChange);                              
    26.         }                                                                                                                
    27.  
    28.  
    29.         if (Input.GetKey("space"))
    30.         {
    31.             rb.AddForce(jumpForce * Time.deltaTime, jumpForce, 0, 0);
    32.         }
    33.      
    34.      
    35.         if (rb.position.y < -1f)
    36.         {
    37.             FindObjectOfType<GameManager>().EndGame();                                                                    
    38.         }
    39.  
    40.      
    41.      
    42.     }
    43.  
    44. }
    45.  
     
    Last edited by a moderator: Apr 21, 2019
  2. sdwsk

    sdwsk

    Joined:
    Aug 28, 2018
    Posts:
    34
    Try this:
    Code (CSharp):
    1. if (Input.GetKeyDown("space"))
    2.         {
    3.             rb.AddForce(jumpForce * Time.deltaTime, jumpForce, 0, 0);
    4.         }
     
  3. sdwsk

    sdwsk

    Joined:
    Aug 28, 2018
    Posts:
    34
    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
     
  4. csofranz

    csofranz

    Joined:
    Apr 29, 2017
    Posts:
    1,556
    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.
     
    sdwsk likes this.
  5. sdwsk

    sdwsk

    Joined:
    Aug 28, 2018
    Posts:
    34
    I actually mentioned this problem :p

    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:

    Code (CSharp):
    1. Physics.Raycast(transform.position, Vector3.down, Collider.bounds.extents.y + 0.1f);
     
  6. palex-nx

    palex-nx

    Joined:
    Jul 23, 2018
    Posts:
    1,748
    This is classic example of why FSM was invented in first place. Check this out:
    <Link No Longer Valid>