- Home /
Detecting Collision between Charctor controller and Box Collider
Hi, I am trying to detect a collision between my player (The character controller) and a wall object (the box collider). My code looks like this:
void OnCollisionEnter(Collision collision)
{
if(collision.gameObject.tag == "This")
{
Debug.Log ("Player Hit the Wall");
}
The script is placed on the player, and the wall is tagged "This". Anyone have ideas why nothing is showing up in the console when the player hits the wall?
Any help is much appreciated, I'm sure the answer is painfully obvious.
Thank you.
The best way to see what's going on is put a Debug.Log() to show what it's colliding with before the if statement to make sure it actually is colliding first. Let us know what the results are once you've established the collision happens.
You could include the tag in the output of that Debug but I'd start off just showing some text like "Hit" to prove it works.
You mean I should put 'Debug.Log' before the if statement above? If the debug is not within the IF statement , what would trigger it? I'm not sure I get what you mean. I am just trying to make sure the collision is detected. There is nothing else in my scene that would trigger a collision.
I have put this piece of code on a separate cube with a box collider and a rigidbody and it works fine. But I cannot figure out how to make it work on the player. I think it's something to do with the character controller?
Just diagnostics, all I want to do is prove the collision happenas and rule out any issue with the tag, that's all. Copy that Debug but make it obvious which one is reporting so...
Debug.Log("Player hit wall - debug from before if");
I dropped that in, but no joy :(
I get the same result as before, nothing appearing in the console. it doesn't seem to be registering any collision at all, my code is currently:
void OnCollisionEnter(Collision collision)
{
Debug.Log("Player hit wall - debug from before if");
if(collision.gameObject.tag == "This")
{
Debug.Log ("Player Hit the Wall");
}
}
O$$anonymous$$ so we can rule out the Tag. That's good & you say you've got a rigidbody on there, so let's try another common one before we resort to screen shots. Have either of the colliders got isTrigger ticked?
Answer by HarshadK · Jan 05, 2015 at 03:04 PM
You need to use OnControllerColliderHit to detect collisions for character controller.
Is there no way to use OnCollisionEnter? $$anonymous$$y character does not use $$anonymous$$ove nor simple$$anonymous$$ove to get around.
I tried substituting OnControllerColliderHit into my code like so:
void OnControllerColliderHit(ControllerColliderHit hit)
{
Debug.Log("Player hit wall - debug from before if");
if(hit.gameObject.tag == "This")
{
Debug.Log ("Player Hit the Wall");
}
}
But still no result, nothing appears in the console.
Answer by Caolan · Jan 08, 2015 at 01:50 PM
Sorry guys I figured out a work around that I'm going to use. I can't detect collision between a character controller and box collider using OnCollisionEnter as HarshadK stated above, although I can't seem to get OnCharacterColliderHit to work either. (possibly because I am not using a Move function in my player's movement code)
So what I have done instead, and it seems to be working so far, is attaching a box collider to my player aswell as a character controller. I also added a rigidbody to my wall as well as it's own box collider. After doing this my original code seems to work just fine.
It's not an ideal solution, and I'm sure there are a million better ways to do what I am doing but sure look, as long as it works right?
Thanks so much for helping me find an answer chaps, appreciate it.
Your answer
