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

Why can't I jump after my character touches a Rigidbody

Discussion in 'Physics' started by JoeStinky, Jun 23, 2021.

  1. JoeStinky

    JoeStinky

    Joined:
    Jun 22, 2021
    Posts:
    4
    ------------------------------------------------------------------------------------------------------------------
    I'm new to unity and while playing around with the editor, I noticed that my ability to jump disappeared after i touched another object with it's own rigidbody and was only activated after i touched something else besides the floor. I checked online and it looks like there are only solutions for Unity 2D so I came here.
    Here is my code
    -------------------------------------------------------------------------------------------------------------------
    ______________________________________________________________________________________
    Code (CSharp):
    1.  
    2. using System.Collections;
    3. using System.Collections.Generic;
    4. using UnityEngine;
    5.  
    6. public class Player : MonoBehaviour
    7. {
    8.  
    9.     private bool jump;
    10.     private bool groundCheck;
    11.     private float h;
    12.     private float v;
    13.     private Rigidbody rb;
    14.  
    15.  
    16.     void Start()
    17.     {
    18.         rb = GetComponent<Rigidbody>();
    19.     }
    20.  
    21.     void Update()
    22.     {
    23.         if (Input.GetKeyDown(KeyCode.Space))
    24.         {
    25.             jump = true;
    26.            
    27.         }
    28.         h = Input.GetAxis("Horizontal");
    29.         v = Input.GetAxis("Vertical");
    30.     }
    31.     void FixedUpdate()
    32.     {
    33.  
    34.         rb.velocity = new Vector3(h * 10, rb.velocity.y, v * 10);
    35.  
    36.         if (!groundCheck)
    37.         {
    38.             return;
    39.         }
    40.         if (jump)
    41.         {
    42.             rb.AddForce(Vector3.up * 8, ForceMode.Impulse);
    43.             jump = false;
    44.         }
    45.        
    46.        
    47.        
    48.        
    49.  
    50.      }
    51.     private void OnCollisionEnter(Collision collision)
    52.     {
    53.         groundCheck = true;
    54.     }
    55.  
    56.     private void OnCollisionExit(Collision collision)
    57.     {
    58.         groundCheck = false;
    59.     }
    60.    
    61.  
    62. }
    63.  
     
  2. Tarodev

    Tarodev

    Joined:
    Jul 30, 2015
    Posts:
    181
    I'm assuming it's because your ground check is not verifying if it's a ground object and you're just saying false on every exit.
    Tag your ground and then in your ground checks do something like this:
    Code (CSharp):
    1.  private bool groundCheck => groundCollisions >= 0;
    2.     private int groundCollisions;
    3.  
    4.     private void OnCollisionEnter(Collision collision) {
    5.         if (collision.transform.CompareTag("Ground")) groundCollisions++;
    6.     }
    7.  
    8.     private void OnCollisionExit(Collision collision)
    9.     {
    10.         if (collision.transform.CompareTag("Ground")) groundCollisions--;
    11.     }
     
    JoeStinky likes this.
  3. JoeStinky

    JoeStinky

    Joined:
    Jun 22, 2021
    Posts:
    4
    It fixed the problem but I can now jump infinitely. I'm sure i can find something out to fix that though. Thanks.
     
  4. JoeStinky

    JoeStinky

    Joined:
    Jun 22, 2021
    Posts:
    4
    Never mind I fixed it by changing the 0 that groundCollision was >= to a 1 and it works now. Thanks for the help.
     
  5. Tarodev

    Tarodev

    Joined:
    Jul 30, 2015
    Posts:
    181
    Ahhh, yes. That was my mistake. Glad it helped you
     
    JoeStinky likes this.