Alternative for OnControllerColliderHit?

After messing around with it for a while, I determined that OnControllerColliderHit only works whenever the character controller itself is in motion, and not if it is stationary and gets hit with something else. I want to be able to detect when a rigid body object such as a rock/snowball etc hits a player. The end goal will be to have the character get knocked back or take damage or something, which obviously won’t work if the player is smart enough to stand still. Any idea what I can do?

For those that like code, this is how I’m using it so far:

Think I was just trying to make it hard on myself… I just attached another capsule object, disabled its renderer and am using that to check for the collisions instead. I’m assuming I can use something like collision.contacts[0].normal to calculate knock back?

I don’t know how common this practice is, but I usually make the CharacterController vibrate by a very tiny (not visible) amount each frame. Super ugly, but it works if you want to avoid the physics. I also keep track of which objects gained and lost contact to the CC between each frame, and once a state changes I dispatch a SendMessage on that game object so it can react accordingly.

Having difficulty getting the knockback working…
I’m using a slightly modified version of the FPSWalkerEnhancedFPSWalkerEnhanced, and it took me a while to realize there was even a problem. Basically i get the normal of the collision in the manner I described above, multiply the x and z values by -1 to get their opposite, and do this in the walker script right before gravity is added:

note: knockBack is a Vector3 of the collision normal multiplied by (-1,0,-1);

You can see the results for yourself in this slightly outdated version: http://filer.case.edu/nrc10/MP_test/WebPlayer.html

Basically, it works fine if the player is looking at the oncoming snowball, but if they are looking in a different direction then the knock-back force is applied with the same normal but with respect to the direction they are currently looking. And ideas?

You’re making this more complicated than it needs to be, methinks :slight_smile:

Yes, if a rigidbody hits a CharacterController, the only time OnControllerColliderHit() will fire is when the CC is performing a move. But OnCollisionEnter() will always fire on that rigidbody… so handle the knockback in a script on your snowballs, not the player.

eg:

function OnCollisionEnter(collision : Collision) {
    //if we hit a character controller
    if(collision.gameObject.GetComponent(CharacterController)){
        collision.gameObject.SendMessage("KnockBack");
    }
}

As for the knockback stuff, I outlined a solution to this entire concept in this thread.