Boolean not toggling, but debug reporting correctly.
I'm trying to make sure my character/player cannot jump while in the air, so I'm detecting collision with the ground using tags and flipping a boolean to enable or disable jumping accordingly. Unfortunately, even though the debug.log statements are coming through as though the collisions are correctly detected, the jump button is still unable to work.
Here's my script:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlayerJump : MonoBehaviour
{
public Rigidbody rb;
public float jumpForce;
private bool touchingGroundBool = false;
private void OnCollisionStay (Collision collision)
{
if (collision.collider.tag == "Ground")
{
touchingGroundBool = true;
Debug.Log("touching the ground");
}
}
private void OnCollisionEnter(Collision collision)
{
if (collision.collider.tag == "Ground")
{
touchingGroundBool = true;
Debug.Log("touching the ground");
}
}
private void OnCollisionExit (Collision collision)
{
if (collision.collider.tag == "Ground")
{
touchingGroundBool = false;
Debug.Log("NOT touching the ground");
}
}
public void JumpButton()
{
if (touchingGroundBool)
{
Debug.Log("pushing up on cube with " + jumpForce + " force");
rb.AddForce(0, jumpForce, 0);
}
else if (!touchingGroundBool)
{
Debug.Log("Unable to jump!");
}
}
}
and here's what my log looks like when I press Jump and I'm touching the ground:
I've tried reading other people's threads on this but as far as I can tell I'm doing everything right based on the answers they've gotten. My assumption is I'm doing something wrong with the code that just isn't being picked up by VS or Unity as an error. I'm new to both so if I'm making some dumb mistakes aside from what's not working here as well, please let me know.
Your answer
