- Home /
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! :D
Answer by JeremyG · Feb 03, 2013 at 09:44 AM
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?
Answer by schaddemm · Feb 02, 2013 at 10:42 PM
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.
Answer by Loius · Feb 02, 2013 at 11:19 PM
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.
This is the only script I've written so I don't have my own movement commands implemented, just the stuff based off the script I linked to in my original post.
This technique worked perfectly in Unity 3.5.x, so I'm thinking it's definitely something in the update that's causing issues. It's just frustrating to know that I had it working in the previous version. Personally, I was hoping it would be a fault in my code, but that doesn't appear to be the case. :P
I'll just look into writing my own isgrounded function like schaddemm suggested, and let everyone know what works or not. If it works, I'll be sure to post it so that if anyone else encounters this problem, the answer won't be far away! :)
O$$anonymous$$, so I posted a thread 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! $$anonymous$$ore 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.
Your answer
Follow this Question
Related Questions
How to make player not fall during dash 1 Answer
Moving Platform and CharacterController.Move not working. The player falls 3 Answers
Simulating the Character Controller? 0 Answers
Character Controller Falls through Mesh Collider ground and objects in 3.1 2 Answers
Checklist: Object or Character is falling through the floor 12 Answers