x


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.

more ▼

asked Dec 14 '09 at 12:59 AM

Grim420 gravatar image

Grim420
30 4 4 8

(comments are locked)
10|3000 characters needed characters left

2 answers: sort voted first

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
    }
}
more ▼

answered Dec 14 '09 at 06:06 AM

Jaap Kreijkamp gravatar image

Jaap Kreijkamp
6.4k 20 26 70

(comments are locked)
10|3000 characters needed characters left

You can check the type of the incoming collider using the GetType method:-

if (collider.GetType() == CharacterController) {
    ...
}
more ▼

answered Jan 11 '10 at 09:19 PM

andeeee gravatar image

andeeee ♦
1.4k 3 6 18

(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x5081
x2497

asked: Dec 14 '09 at 12:59 AM

Seen: 3046 times

Last Updated: Dec 14 '09 at 12:59 AM