- Home /
Check for a CharacterController collision from a non-CharacterController object
So I've been learning about collision detection and I think I get it for the most part.
I can use a script with OnCharacterControllerHit on my CharacterController to detect if it hit anything, and I can use a script with OnCollisionEnter on an object to see if another object hits it. This is all fine, but I seemed to have hit a snag when I try to put a script on an object to see if a CharacterController hits it.
Does this make sense? I'm trying to write a script for all the switches in my level. One script that I put on each switch and it checks for what switch its attached to and if the player touches it. It seems that I have to check for the collision with a script on the CharacterController and then send some kind of signal to the switch script.
Is there a shorter way to do that? I'm trying to keep the scripts as simple as possible. Maybe something like using my switch script to GetComponent.CharacterController.collider.hit from the CharacterController script and checking the objects that the controller hit... or something along those lines.
How do I check for a CharacterController collision from a different object?
Thanks.
Answer by Jaap Kreijkamp · Dec 14, 2009 at 06:06 AM
I think it would be easier to give your player a good tag like 'Player', make the switches trigger Colliders (just check isTrigger in collider properties), and in the code of your switches have something like:
function OnTriggerEnter(col : Collider) {
if (col.gameObject.tag == "Player") {
// do fancy stuff
}
}
Answer by andeeee · Jan 11, 2010 at 09:19 PM
You can check the type of the incoming collider using the GetType method:-
if (collider.GetType() == CharacterController) {
...
}