testing if player is grounded

Hi there guys, I just want to know if this is a good way to test if my player is grounded. Is there a better way with maybe some performance advantage or is this fine.

function OnCollisionEnter(collisionInfo : Collision) 
{    
	if (collisionInfo.gameObject.tag == "ground" )        
		{        
		isGrounded = "true" ;
		print("on the ground");        
		}
}

function OnCollisionExit(collisionInfo : Collision) 
{    
	if (collisionInfo.gameObject.tag == "ground" )        
		{        
		isGrounded = "false" ;
		print("in the air");        
		}
}

Thanks

Tiaan

If there is a Character Controller component attached to your player, you can just use isGrounded boolean.

function Update () {
    var controller : CharacterController = GetComponent(CharacterController);
    if (controller.isGrounded)
    {
        print("We are grounded");
    }
}

Thanks Mortiis

i don’t have a character controller. My player has a jetpack and I use slightly different movements from the character controller. Would it still be better to attach that?

Maybe if you’re not using character Controller the best approach is using raycast with -Vector.up and the threshold = to the player height/2. Hope this can help you

Well then, character controllers Move and SimpleMove functions may be more expensive than what you currently use. Your first script looks just fine to me, you can use profiler to see if it has any impact on performance when the character collides with the “ground” tagged objects but I don’t think if there will be any difference in the performence at all.