- Home /
Collision CharacterController vs CharacterController
Hi, I have my player character defined with a CharacterController, and I move my enemies the same way (with other CharacterControllers). The thing is, I was thinking about using OnControllerColliderHit for when my character is hit by enemies, so that he goes back hurt.
The problem is that OnControllerColliderHit seems to trigger on just when it hits a Collider, and as my enemies don't have a collider, but a CharacterController to move them, it's not working!
Is there a way around this? I don't want to change my enemies from being CharacterControllers, because both enemies and main character inherit from the same class (which has a CharacterController of course).
Thanks a lot!!
I just realized that CharacterController.collisionFlags is working correctly, but I need more information, because I need the normal of the collision point to push my main character back, not just if the collision is from the side, above or below. Any ideas?
It's veeery wierd that both OnControllerColliderHit and OnCollisionEnter aren't working for CharacterController vs CharacterController when the very CharacterController inherits from Collider, which is $$anonymous$$ADE FOR THAT!! oO
Answer by Zeldarulah · May 24, 2012 at 02:58 PM
Why not set enemies to have a certain tag like "Enemy", and do an OnTriggerEnter(Collider col) for your character, wherein you use:
if (col.gameObject.tag == "Enemy") {
//whatever you wanted to do here
}
Because I need more information (I want to push the main character in the direction it has been hit) so collider doesn't give me that. Collision would, or ControllerColliderHit, but they don't seem to be wokring...
Ah. Well, I'm new to Unity, but, if your enemies are 'targetting' your player, perhaps you could create an accessor method in enemy to that, and then use a GetComponent to use their accessor, and use that vector to push your player back.
Answer by aldonaletto · May 24, 2012 at 05:49 PM
You're right: that's a weird thing to not have a character-character collision event! But maybe @zeldarulah's suggestion can do what you want: add a trigger volume to the character and a kinematic rigidbody (moving triggers need it to work); when the collision happens, calculate a "fake normal" based on the position of both characters - like this:
function OnTriggerEnter(other: Collider){ var normal = (transform.position - other.transform.position).normalized; // this fake normal shows the direction from the other to this object, // somewhat like hit.normal would do (if it worked, of course) }Notice that you must add the collider to the character (Unity asks if you want to add or replace the current collider).
Thanks a lot! I surrendered yesterday and started doing another thing because nothing was working, but I'll try tonight or tomorrow and tell you how it went. From what I've seen in another thread, this volume should be a child of the object, like a sphere or something...
In this case it will be easier to add a trigger ins$$anonymous$$d of childing a trigger object to the character: select the prefab and click menu Component/Physics/Capsule Collider - Unity will ask if you want to replace or add the collider: click Add, and the capsule collider will be added to the character without removing the CharacterController. Set the capsule collider Is Trigger field to make it a trigger, and adjust its radius in the Inspector to make it larger than the CharacterController. Add the OnTriggerEnter code to some script attached to the character, and voilà: it's ready.
If only the enemy applies damage to the player when hitting it, add the OnTriggerEnter event and the trigger only to the enemy prefab.