I recently posted this in Unity Answers but I’m hoping I might find some more information over here.
The problem I am having is with the default isGrounded variable returning BOTH false and true, no matter what’s going on. Here’s a simple script that I wrote in C# as a test. Run it and see if you have the same issue I’m having. Have your console open for more info!
using UnityEngine;
using System.Collections;
public class isGroundedTest : MonoBehaviour
{
// Use this to see results directly in editor!
public bool grounded;
// Update is called once per frame
void Update ()
{
// Grab instance of CharacterController
CharacterController mainPlayer = GetComponent<CharacterController>();
// Check if player is grounded!
if(mainPlayer.isGrounded == true)
{
grounded = true;
Debug.Log ("You are on ground!");
}else
{
grounded = false;
Debug.Log ("You are NOT on ground...");
}
}
}
Basically, the “grounded” variable fluctuates between TRUE and FALSE in the editor, even while the player is standing perfectly still on the ground, not jumping or anything. Sometimes during Update(), it would seem that even if the player falls, if the game thinks it’s on the ground, it won’t update the “grounded” variable until it collides with the ground again.
At first, I thought this was a problem unique to Unity 4, so I “downgraded” to 3.5.6, but I still get the same result. This is unfortunate, because I use this variable for many functions!
I had this working perfectly a few months ago, but I lost everything due to a hard drive crash, and while replacing the drive I decided to upgrade to Windows 8 64-bit*. That’s the only thing I’ve changed about my computer, and the drivers are up to date and everything, but I can’t figure out why Unity’s isGrounded function would suddenly freak out just because I updated my operating system. Is anyone else having any issues with this?
Also, I would swear I didn’t use to have to manually assign the animation files to the 3rd Person Controller prefab until Unity 4, but Unity 3 is having me do that now, too… maybe they started doing that in 3.5.6f4? I dunno. Any ideas/help would be immensely appreciated!
I guess I’ll check for some updates, maybe there’s something screwy with my graphics card’s drivers and they need updated. I will let anybody know if I get this issue fixed, in the meantime any more ideas would be appreciated!
EDIT: Nope, that didn’t do anything.
It’s sort of a shame this isn’t a wide-spread bug, as at least then there would be more information on this! As far as I can tell, no one else has any troubles with isGrounded - I can’t find any threads on it (at least about this particular issue). I’ve tried deleting all of the old Unity files, re-installing, uninstalling, installing an older version, deleting all files from that, and nothing works.
Maybe this is just a sign I should finally get a Mac.
OK, so I did some more testing and searching, and it seems that I’m having issues with Unity’s physics engine!
I copy>pasted some code that wrote one’s own isGrounded() function by casting a ray, and I had the same exact results as with the default isGrounded function!
Here’s my code now, this time in JavaScript:
#pragma strict
var distToGround: float;
public var grounded: boolean;
function Start()
{
// get the distance to ground
distToGround = collider.bounds.extents.y;
}
function IsGrounded(): boolean
{
return Physics.Raycast(transform.position, -Vector3.up, distToGround + 0.1);
}
function OnControllerColliderHit(col: ControllerColliderHit){
if (IsGrounded() == true)
{
grounded = true;
Debug.DrawRay(col.point, col.normal, Color.blue);
Debug.Log("GROUND!");
} else
{
grounded = false;
Debug.DrawRay(col.point, col.normal, Color.red);
Debug.Log("AIR");
}
}
function Update ()
{
}
Now, I can clearly see that the raycast changes from blue to red almost every other frame, although sometimes it’ll be one or the other for AGES before randomly updating again, and the console confirms the random changes with the messages"GROUND" or “AIR”, as well as the viewable Grounded variable in the inspector.
So, it seems that for some reason, Unity’s physics engine doesn’t seem to like my setup. Does ANYONE have any ideas as to what’s going on here?
“Now, I can clearly see that the raycast changes from blue to red almost every other frame”
Considering the raycast only checks once per collision, this also means that OnControllerColliderHit is being called nearly every frame for some reason.
I’m sure you’ve probably already checked this, but my first guess would be that there is some Bounciness on one of the physic materials which is causing the collider to not ever stay on the ground.
Also check the “minimum penetration for penalty” setting in your physics settings. Might happen if it is set to a very low value.
Also disengage your player controller script to make sure it is not adding some small erroneous Y movement every frame.
I tried all of those suggestions and it still seems to happen. This is really, really weird. I’m beginning to think I should just install Windows 7 on a separate partition and see if that has ANYTHING to do with it; if it does, then I’ll do more research and see what solutions there are.
For whatever reason, it seems that the default 3rd Person Controller cannot rely on “isGrounded” - I don’t why this is, there must be some code in it that changes the y position just slightly every frame? Either way, it seems faulty for some reason.
And then it hit me - why don’t I test out “isGrounded” with the 1st Person Controller?
I did, and the results were 100% constant, just like they should be! When I was on the ground, it reported it as such, and same for the air. Yay!
Unfortunately, this was after I decided to wipe my hard drive and reinstall Windows 7 to see if that fixed anything (it didn’t), so next time I’m gonna be a little more sleuthy in my searches for an answer.
Somewhat unrelated but a question I feel needs answered anyway is this; I thought you never used to have to manually assign the animation clips for the 3rd Person Controller in Unity3.5.x? Or register the software for that matter, but it seems that I needed to do both of those things. Could the 3rd Person Controller perhaps have been updated in Unity’s servers, and now it has a bug in it and that’s why I’ve had such troubling issues?