- Home /
Collision detection between box collider and character controller
Hi
I am new on unity3d i am facing a problem on collision with my character having character contorller to an object having box collider. I have found that character controller collision can be detected using
void OnControllerColliderHit(ControllerColliderHit hit)
but this is only work and called in script which is attached with character having character collision so when i use the onControllerColliderHid it call every time which ever thing is collide with it like if my character is walking on road it called. So now what basically i want is to detect collision onCollisionEnter in the script which is attached with box having box collider and when the character collide with that box that onCollisionEnter should called but it's not calling due to character controller on my character if i add box collider on my character then it always call. However if i make the box collider istriggered then OnTriggerEnter called either character have character controller or box collider but it's not working if i just uncheck the triggered. so can any one help me in this
I think you need to rephrase your question more clearly because I have read it a couple times and I am still not sure whether I have understood what your are asking for.
From my understanding, you have it right. If you have checked the isTrigger
property in the box collider, then you need to implement the OnTriggerEnter
, which will be fired every time a Collider
enters the trigger. What's the problem in doing that?
Answer by aldonaletto · Oct 07, 2013 at 03:15 PM
Yes, OnControllerColliderHit is only sent to the CharacterController's script, and only when the character hits the collider (nothing happens when the collider hits the character). If you want to call some function in the other object's script, use SendMessage in the character script:
void OnControllerColliderHit(ControllerColliderHit hit){
hit.transform.SendMessage("SomeFunction", SendMessageOptions.DontRequireReceiver);
}
This code calls the function SomeFunction in the other object's script, if it exists. In order to improve performance, it would be better to SendMessage only to the desired object - by comparing its tag, for instance:
void OnControllerColliderHit(ControllerColliderHit hit){
if (hit.transform.tag == "SomeTag"){
hit.transform.SendMessage("SomeFunction", SendMessageOptions.DontRequireReceiver);
}
}
Is there is no other option that character controller hit detect directly on the objects which it collide onCollideEnter or any other method ?
It depends on what you want to do. OnCollision events are sent to both objects, but they only occur when a rigidbody hits a collider or CharacterController, not when the CharacterController hits a rigidbody. OnTrigger events are also sent to both objects, and occur when a rigidbody or CharacterController hits the trigger, or when the trigger has its own rigidbody (usually kinematic) and is hit or hits any collider - but a trigger is "transparent": it doesn't constrain the other object's movement. OnControllerColliderHit is sent only to the character's script, and only happens when the character hits other collider. Due to the contact with the ground, this event occurs almost every physics cycle - that's why it's a good idea to filter out these collisions, leaving only the ones of interest.