Search Unity

On Ground broken

Discussion in 'Scripting' started by Kaperosa, Feb 14, 2018.

  1. Kaperosa

    Kaperosa

    Joined:
    Feb 12, 2018
    Posts:
    12
    The On Ground check box won't stay checked. Every time I jump once it automatically turns off. After exporting it and playing it my character is only able to jump once.
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class Jump : MonoBehaviour{
    6.  
    7.     public bool onGround;
    8.     private Rigidbody rb;
    9.  
    10.     void Start ()
    11.     {
    12.         onGround = true;
    13.         rb = GetComponent<Rigidbody> ();
    14.     }
    15.  
    16.     void Update()
    17.     {
    18.         if(onGround)
    19.         {
    20.             if(Input.GetButtonDown("Jump"))
    21.             {
    22.                 rb.velocity = new Vector3 (0f, 7f, 0f);
    23.                 onGround = false;
    24.             }
    25.         }
    26.     }
    27.     void OnCollisionEnter(Collision other)
    28.     {
    29.         if(other.gameObject.CompareTag("ground"))
    30.         {
    31.             onGround = true;
    32.         }
    33.     }
    34. }
    35.  
    36.  
     
  2. fire7side

    fire7side

    Joined:
    Oct 15, 2012
    Posts:
    1,819
    Your ground has to have a collider on it, and your character has to have a collider as well as the rigidbody it appears to already have.
     
  3. Baste

    Baste

    Joined:
    Jan 24, 2013
    Posts:
    6,338
    You're never getting an OnCollisionEnter message where the thing you're colliding with has the tag "ground".

    You should add a debug message to your OnCollisionEnter to see what's going on:

    Code (csharp):
    1.  
    2.     void OnCollisionEnter(Collision other)
    3.     {
    4.         Debug.Log("Collided with " + other.gameObject.name + ", which has the tag " + other.gameObject.tag, other.gameObject);
    5.         if(other.gameObject.CompareTag("ground"))
    6.         {
    7.             onGround = true;
    8.         }
    9.     }
    10.  
    Since I added other.gameObject as the second argument to Debug.Log, you can click the log message to get the thing you're collided with pinged in the hierarchy.
     
  4. Joe-Censored

    Joe-Censored

    Joined:
    Mar 26, 2013
    Posts:
    11,847
    My guess is you have spelled the "ground" tag wrong in your comparison in OnCollisionEnter. Maybe you spelled it "Ground" or something.