- Home /
How to find the gameobject that my character controller is grounded on?
I need to find the "Ground" object that my character controller is standing on whenever i use "isGrounded". I know Raycasting down is teh most logical one.. but am trying to avoid unnecessary raycasting expenses for this purpose..
I just tried OnControllerColliderHit to find the collider object, and checked teh normal.y to make sure if the object is below teh controller, and assume it is teh ground object. But this gets called every frame.. Not like teh OnCollisionEnter/Exit which only executes once per collision. Is this teh right approach? Will it be expensive? Or will this function be called anyways?
Answer by amphoterik · Aug 01, 2013 at 11:55 AM
You can use OnControllerColliderHit to catch any bottom collisions each frame: http://docs.unity3d.com/Documentation/ScriptReference/CharacterController.OnControllerColliderHit.html
I suppose that OnControllerColliderHit isn't an expensive choice: the collision tests are performed each physics cycle anyway, and the fields passed in the ControllerColliderHit seem to be directly obtained from the collision test (point, normal, collider etc.)
Answer by TSRajesh · Aug 01, 2013 at 12:14 PM
Thanks. I got as much. I also gather that the OnControllerColliderHit will be called in any case, and there is not much of an overhead. This piece of code works for me.
void OnControllerColliderHit(ControllerColliderHit hit)
{
if (hit.normal.y > 0.9f) //Change if we want incline
myGround = hit.collider.gameObject;
}
.
Your answer
![](https://koobas.hobune.stream/wayback/20220613111334im_/https://answers.unity.com/themes/thub/images/avi.jpg)