Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

How To Prevent Multiple Jumps In 2D C# Game

Discussion in 'Scripting' started by Kakapio, Dec 6, 2015.

  1. Kakapio

    Kakapio

    Joined:
    Nov 8, 2014
    Posts:
    9
    Hey,
    I've created a 2D game with basic movement. The jump function works, but allows for infinite jumping. What're some simple ways I can use to make sure the player is grounded before allowing them to jump? Do you have any other solutions? I've tried to follow a few tutorials, but they didn't work too well. Thanks.

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3. public class PlayerController : MonoBehaviour
    4. {
    5.     private float moveSpeed = 5f;
    6.     private float jumpHeight = 300f;
    7.     private Rigidbody2D rigidBody;
    8.     // Use this for initialization
    9.     void Start()
    10.     {
    11.         rigidBody = GetComponent<Rigidbody2D>();
    12.     }
    13.     // Update is called once per frame
    14.     void Update()
    15.     {
    16.         if (Input.GetAxisRaw("Horizontal") > 0.5f || Input.GetAxisRaw("Horizontal") < -0.5f)
    17.         {
    18.             transform.Translate(new Vector3(Input.GetAxisRaw("Horizontal") * moveSpeed * Time.deltaTime, 0f, 0f));
    19.         }
    20.         if (Input.GetKeyDown(KeyCode.W))
    21.         {
    22.             rigidBody.AddForce(new Vector2(0, jumpHeight));
    23.         }
    24.     }
    25. }
     
  2. Fra123X

    Fra123X

    Joined:
    Mar 10, 2013
    Posts:
    40
    You need to make sure the player is grounded.
    There different ways of doing so, one could be to check if the player is colliding with something underneath him and if so, allow the jump.
    Another way could be to cast a 2d raycast down and check if the distance to the closest collider is 0 (or a really small value).
    Once you find the most suitable way of checking it, just apply that value to a bool called "isGrounded" and change the

    Code (CSharp):
    1.  
    2. if (Input.GetKeyDown(KeyCode.W))
    3. {
    4.     rigidBody.AddForce(new Vector2(0, jumpHeight));
    5. }
    6.  
    To
    Code (CSharp):
    1.  
    2. if (Input.GetKeyDown(KeyCode.W) && isGrounded)
    3. {
    4.     rigidBody.AddForce(new Vector2(0, jumpHeight));
    5. }
    6.  
     
    JoeStrout likes this.
  3. JoeStrout

    JoeStrout

    Joined:
    Jan 14, 2011
    Posts:
    9,848
    I show not one but three (count 'em!) different ways of animating a 2D character with limited multiple jumps (my demo allows exactly one, but zero would be even easier) in this article.

    HTH,
    - Joe
     
  4. phaem

    phaem

    Joined:
    Jan 5, 2015
    Posts:
    76
    Previous answers give you idea on how to check if player is grounded. To prevent double jumping you should use the state machine, or you'll face consequences described here soon.