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

Question How do I stop infinite jumping?

Discussion in 'Scripting' started by UnamedGameDev, Dec 28, 2022.

  1. UnamedGameDev

    UnamedGameDev

    Joined:
    Jul 2, 2022
    Posts:
    2
    So I'm a beginner that is dealing with an infinite jumping problem, and I've looked through some forums before this, and I still can't figure it out.
    Here's my code:

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class PlayerMovement : MonoBehaviour{
    6.  
    7.     public float speed = 10f;
    8.     CameraMovement cameraPlace;
    9.     bool touchingFloor;
    10.     public Collider floorCol;
    11.    
    12.     void Start(){
    13.         cameraPlace = FindObjectOfType<CameraMovement>();
    14.         cameraPlace.rotation = transform.rotation;
    15.         touchingFloor = true;
    16.     }
    17.  
    18.     void Update(){
    19.  
    20.         Vector3 input = new Vector3 (Input.GetAxisRaw("Horizontal"), 0, Input.GetAxisRaw("Vertical"));
    21.         Vector3 direction = input.normalized;
    22.         Vector3 velocity = direction * speed;
    23.         Vector3 moveAmount = velocity * Time.deltaTime;
    24.  
    25.         transform.Translate(moveAmount);
    26.  
    27.         float yValue= cameraPlace.cameraRot.y;
    28.         cameraPlace.cameraRot = new Vector3(0f, yValue, 0f);
    29.         transform.eulerAngles = cameraPlace.cameraRot;
    30.         cameraPlace.rotation = Quaternion.Euler(cameraPlace.cameraRot);
    31.  
    32.         if(touchingFloor = true && Input.GetKeyDown(KeyCode.Space)){
    33.             Vector3 v = GetComponent<Rigidbody>().velocity;
    34.             v.y = 4.2f;
    35.             GetComponent<Rigidbody>().velocity = v;
    36.             touchingFloor = false;
    37.         }
    38.  
    39.         OnColliderEnter(floorCol);
    40.  
    41.     }
    42.  
    43.     void OnColliderEnter(Collider floorCol){
    44.         if (floorCol.gameObject.tag == "floor"){
    45.             touchingFloor = true;
    46.         }
    47.  
    48.     }
    49. }
     
  2. TzuriTeshuba

    TzuriTeshuba

    Joined:
    Aug 6, 2019
    Posts:
    185
    Hey, so it may be helpful to provide some more context like does the rigidbody component have isKinematic checked? also maybe describe the "infinite jumping", is it one jump after the other or one infinitely long jump?

    There are some candidate issues in your code, that may not be the cause of your bug, but you may want to handle as they will easily be a bug at some point.

    Look into the difference between Update and FixedUpdate, and more generally, the seperation between the physics callback functions (FixedUpdate, OnCollisionEnter/Exit, OnTriggerEnter/Exit). When it comes to moving an object with a rigidbody, you should choose one of the following:

    1) mark it as kinematic (via code or inspector) and then move it freely using its transform component only. Dont use AddForce() or set it's velocity. The movement should be done in the Update function.

    2) dont mark it as kinematic, but dont move it via its transform. Only move it using the rigidbody and physics engine. even here you shuld be mindful of directly setting its velocity.

    you are calling OnColliderEnter() every frame (every Update()). OnColliderEnter is a callback that is triggerred during the physics step. The unity engine triggers this function on its own when it detects a collision and you generally never call it explicily from your own code (although there is nothing technically wrong with doing so).
    basically, your variable 'touchingFloor' is always equal to true, as the only time you set it to false, you reset it to true immediately in the next line.
     
  3. frenchynyc

    frenchynyc

    Joined:
    May 14, 2015
    Posts:
    28
    It looks like the issue may be with the way you are checking if the player is touching the floor. In the Update function, you are setting the touchingFloor variable to the result of the OnColliderEnter method, which will always be true because it is a method and not a boolean value.

    To fix this, you can update the Update function to check the touchingFloor variable directly instead of calling the OnColliderEnter method. You can also update the OnColliderEnter method to accept a Collision parameter instead of a Collider parameter, and use the collider property of the Collision object to check if the player is colliding with an object with the "floor" tag.

    Here's how you can modify the Update and OnColliderEnter functions to fix the infinite jumping issue:
    Code (CSharp):
    1. void Update()
    2. {
    3.     // ... other code omitted for brevity ...
    4.  
    5.     if(touchingFloor == true && Input.GetKeyDown(KeyCode.Space))
    6.     {
    7.         Vector3 v = GetComponent<Rigidbody>().velocity;
    8.         v.y = 4.2f;
    9.         GetComponent<Rigidbody>().velocity = v;
    10.         touchingFloor = false;
    11.     }
    12. }
    13.  
    14. void OnCollisionEnter(Collision collision)
    15. {
    16.     if (collision.collider.gameObject.tag == "floor")
    17.     {
    18.         touchingFloor = true;
    19.     }
    20. }
    21.  
    With these changes, the player should only be able to jump when they are touching the floor, and should not be able to jump indefinitely.

    I hope this helps! Let me know if you have any other questions.
     
  4. UnamedGameDev

    UnamedGameDev

    Joined:
    Jul 2, 2022
    Posts:
    2
    Thanks to both of you these helped me out a lot. As I said, I am a beginner so pretty much all help is useful in some way.
     
    frenchynyc likes this.