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 Do I Fix Infinite Jumping?

Discussion in '2D' started by Cobble, Nov 2, 2016.

  1. Cobble

    Cobble

    Joined:
    May 9, 2015
    Posts:
    33
    Hi! I'm making a combat system in which a short platform'er puzzle appears on screen when interacting with an enemy. However, while in this battle mode, I cannot seem to prevent the player from infinitely jumping.

    Code (csharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class BattleController : MonoBehaviour {
    5.  
    6.     public float moveSpeed;
    7.     public float jumpHeight;
    8.  
    9.     public Transform groundPoint;
    10.     public float radius;
    11.     public LayerMask groundMask;
    12.  
    13.     bool isGrounded;
    14.     Rigidbody2D rb2D;
    15.  
    16.  
    17.     void Start () {
    18.         rb2D = GetComponent<Rigidbody2D>();
    19.     }
    20.    
    21.  
    22.     void Update () {
    23.         Vector2 moveDir = new Vector2(Input.GetAxisRaw("Horizontal") * moveSpeed, rb2D.velocity.y);
    24.         rb2D.velocity = moveDir;
    25.  
    26.         isGrounded = Physics2D.OverlapCircle(groundPoint.position, radius, groundMask);
    27.  
    28.         if(Input.GetKeyDown(KeyCode.Space) && isGrounded){
    29.                 rb2D.AddForce(new Vector2(0, jumpHeight));
    30.  
    31.         }
    32.     }
    33.  
    34.     void OnDrawGizmos(){
    35.         Gizmos.color = Color.clear;
    36.         Gizmos.DrawWireSphere(groundPoint.position, radius);
    37.     }
    38. }
     
  2. JoeStrout

    JoeStrout

    Joined:
    Jan 14, 2011
    Posts:
    9,848
    When you have a problem like this, it means that your mental model of what's going on doesn't match what's actually going on. You need to gather more information until you figure out where exactly that mismatch is.

    I would start by inserting a line like this around line 30:

    Code (csharp):
    1.   Debug.Log("Jumping because " + groundPoint.position + " hits ground (" + groundMask + ") within " + radius);
    I see you already have gizmos drawing that circle — that's great! Between that and the above debug output, you ought to be able to figure out what's going on. Run the game, jump up five or six times into the air, far from the ground, and then pause the game. Study the gizmo and the debug output, and the state of the scene. And in particular, check your layers... I think there must be something in the area there marked with the ground layer, that you didn't intend.
     
  3. Redden44

    Redden44

    Joined:
    Nov 15, 2014
    Posts:
    159
    Do you mean that your character is not falling?
    In this case you should check the gravity and if you have flagged the rigid body as kinematic.
     
  4. Cobble

    Cobble

    Joined:
    May 9, 2015
    Posts:
    33
    Adding the Debug didn't seem to work.

    [EDIT]: Omg! I got it to work! Changed a few stuff here and there, and it's working! On top of that, I think I understand how it works, too. Thanks!
     
    Last edited: Nov 3, 2016
  5. JoeStrout

    JoeStrout

    Joined:
    Jan 14, 2011
    Posts:
    9,848
    That's great! Are you going to share the solution for posterity?
     
  6. Cobble

    Cobble

    Joined:
    May 9, 2015
    Posts:
    33
    If I remember correctly, I made a new Layer called "Ground" and applied it to the platform, and kept the background as "Default". I also switched the Ground Mask on the player to "Ground".
     
    JohnRussell likes this.
  7. JoeStrout

    JoeStrout

    Joined:
    Jan 14, 2011
    Posts:
    9,848
    Ah, so it was a layer mask issue. That'd do it. Again, good job figuring that out!