isGrounded not working

I have an extremely simple scene with the following setup.

1 cube object starts in the air and has a Box Collider, Character Controller, Rigidbody (with freeze rotations checked) and a Jump script I will get to later.

Beneath it (with a fair amount of space between it and the hovering cube) there exists a floor cube. This floor cube has a box collider.

Neither of the two objects box colliders have isTrigger checked.


The Jump script looks as follows:

using UnityEngine;
using System.Collections;

public class Jump : MonoBehaviour {

	public float JumpPower;
	private CharacterController controller;
	
	void Start()
	{
		controller = GetComponent<CharacterController>();
	}
	
	void Update()
	{
		if(Input.GetKeyDown("space"))
		{
			Debug.Log("space pressed");
			if(controller.isGrounded)
			{
				Debug.Log("grounded");
				gameObject.rigidbody.AddForce(Vector3.up * JumpPower);
			}
		}
	}
}

So when the scene starts, my cube falls to the floor as expected.

Then when I press space, I get the message ‘space pressed’ but not ‘grounded’. It appears that the is.Grounded check always turns up false. Can someone explain why to me? There’s literally no other scripts or objects in the scene. I’m pretty confused.

According to the documentation, this returns true if “Was the CharacterController touching the ground during the last move?” is a true statement. Have you tried moving the character first, as opposed to simply letting it hit the ground. You could write your own isGrounded functionality as well, and that might be the best option.

//not checked for compilation, this is just an example

private bool isGrounded = false;
void OnCollisionEnter(Collision collision)
{
    if(collision.tag == "ground")
    {
        isGrounded = true;
    }
}

you can not use void OncollisionEnter with character controller .on the behalf of oncollisionenter you can use oncontrollercolliderhit

bool isGrounded = false;
void OnCollisionEnter(Collision collision)
{
if(collision.tag == “ground”)
{
isGrounded = true;
}
}
void OnCollisionExit(Collision collision)
{
if(collision.tag == “ground”)
{
isGrounded = false;
}
}

now if you want to do different behaviour for if you hit the side or not I would use multiple colliders on your objects with different tags, you could have 3 stacked up like a hamburger, top, middle, bottom, then you just check if you collide with top, middle or bottom.