- Home /
empty object with player controller attached isnt detecting collisions
as in the title, I have an empty game object with a player controller attached to it. I created another script (C#) that uses the character controller and is also attached to the empty game object. I am calling OnCollisionStay() in my script. However, It is not detecting a collision. Here is my code:
void OnCollisionStay(Collision contact){
normal = contact.contacts [0].normal;
Debug.Log ("collision: " + contact.collider);
Debug.Log ("game object: " + contact.gameObject);
}
the collider that I am expecting is a terrain with terrain collider. However neither of the debugs are showing up. Also I have a debug that is checking the normal vector and it always shows (0.0, 0.0,0.0).
is there something that I am missing. I have been reading through the api for days and I cant figure this one out.
Answer by Kiwasi · Jun 14, 2014 at 06:53 AM
Do you have a RigidBody attached to one of the GameObjects? Collisions don't occur without a RigidBody
No I don't, I thought you weren't supposed to translate rigidbodies. Can you translate them? Does oncollisionstay check rigidbodies to something like a terrain collided? Or do I need 2rigidbodies to create a collision.
sorry for so many questions. I'm still very new to unity
Answer by dsada · Jun 15, 2014 at 05:31 PM
Unfortunately you cant detect the collisions between a character controller and a collider by OnCollisionEnter/Stay/Exit. You must use OnControllerColliderHit(ControllerColliderHit) function to detect these kind of collisions. If you observe in scene view, that is because of the skin width property of the controller. The colliders can never reach each other because of it. But the above function will detect it anyways
So would I call that from the character controller scrpt or can I "get component" and grab the character controller from my script (movement.cs) and call the function there?