Help! Problems with Character.isGrounded in Unity 4.0.1?

Hi everyone,

I want to make a “PlayerManager” script that handles things like death falls, respawns, scores, etc., and I’ve done this before in Unity 3 utilizing the CharacterController based off of a script posted here, but now it doesn’t seem to work in Unity 4. Here’s my version of that same script:

#pragma strict
var fallDistance: double = 3;
var deathFall: double = -15;
var falling: boolean = false; 
var lastY: float;    
private var character: CharacterController;

function Start()
{
  character = GetComponent(CharacterController);
  lastY = transform.position.y;
}

function Update()
{
  	if (character.isGrounded == false)
  	{ 
    	falling = true;
  	} 
  	if (character.isGrounded == true)
  	{
  		falling = false;
    	lastY = transform.position.y;
  	}
  
  	if (falling)
  	{            
  		var fall: double = lastY - transform.position.y;
    	if (fall > fallDistance)
    	{	        
    		fall = 0.0;
    		falling = false;
        	Debug.Log("OUCH!");
    	}
  	}
}

The problem I am having is that the boolean “falling” flashes between states while the game is running - it’s like there’s something wrong in the CharacterController class in which it’s updating between both states, even while the player is perfectly grounded! As a direct result, sometimes the player will fall and the “OUCH” text will not get outputted to the console, even though they fell above the allowed amount. Has anyone else encountered any problems like this, or is there something I’m doing wrong in the script? Although I just tried that original script and it doesn’t work either, so something seems to be afoot! I’m running Windows 8 64bit, if that might have anything to do with it?

If anybody has any solutions I would greatly appreciate it! :smiley:

3 Answers

3

Safest solution is to implement your own isgrounded function. You could make a raycast to the ground. Or you could in I think OnCollisionEnter() test the hit.normal (see character motor script). Or you could use a trigger collider at the feet of your character.

As to why isgrounded isn’t giving constant results, that’s weird. Test with OnCollisionEnter if we really aren’t colliding with anything. I’m especially thinking about stuff like your character colliding with it’s weapon.

What do your move commands look like? Technically you’re not falling until your Y velocity is negative, so a slight physics fluctuation can make your code think it is-isn’t falling at times.

OK, so I posted a [thread][1] about it over in Unity Support, and it seems to be that the Physics engine in Unity isn't updating properly. I've attempted casting a ray to act as a "feeler" for the ground, and it's flashing between states, just like the default isGrounded variable! More information is available in that thread. I'm also now attempting this in Unity 3.5.6f4, just because I'm more comfortable with it. It doesn't matter what version I use, I'm always getting this problem! Very frustrating. [1]: http://forum.unity3d.com/threads/168944-Problems-with-isGrounded-on-Windows-8

Ok, so I just tried it in Unity 3 and it isn’t working there either! What on earth could be the problem with isGrounded? And why is it cross-version? I’ve never had this much trouble with it before. I’ve upgraded to Windows 8, does anybody know if that’s maybe the problem?