0
\$\begingroup\$

I have a player with a CharacterController attached. The player can move around.

There are obstacles which the player can collide with. These obstacles have a BoxCollider and a Rigidbody which is set to "kinematic" as I don't want these obstacles to be affected by physics and they don't move.

Every time the player collides with an obstacle he receives some damage. To do that I used this piece of code on the obstacle.

private void OnCollisionEnter(Collision collision) {

    print("hit");

    if (collision.gameObject.tag == "Player") {    
        collision.gameObject.GetComponent<Player>().TakeDamage(damage);    
        print("hit player");    
    }    
}

But the obstacle doesn't register collision events from the Player.

The player on the other hand is able to receive collision from the obstacle in the OnControllerColliderHit() and I can set the damage received in there but I'm not sure if that's a good way.

\$\endgroup\$
5
  • \$\begingroup\$ Take a look at the Collision Action Matrix at the bottom of this document - it tells you which pairs of colliding objects generate which collision messages. By making your rigidbody kinematic, you've opted out of all OnCollisionEnter messages except those with dynamic rigidbodies (whereas your character contoller likely behaves as a kinematic body for the way it moves). \$\endgroup\$ – DMGregory Jun 9 at 15:32
  • \$\begingroup\$ Thanks you, I get it now. Is there no other way to achieve what I'm trying to do? I tried changing the contact pairs mode in the physics settings to "enable kinematic kinematic pairs" but it didn't work. \$\endgroup\$ – Richard David Jun 9 at 15:42
  • \$\begingroup\$ I don't know what you're trying to do, so I can't answer that yet. Try editing your question to describe the gameplay feature or behaviour you're trying to create, then ask for how to create that feature. I'm willing to bet there's a way to do it. \$\endgroup\$ – DMGregory Jun 9 at 15:43
  • \$\begingroup\$ In short what I'm trying to do is receive damage on the player(Character controller) from the box collider on collision. Hence I'm using OnCollisionEnter() to set the damage (which doesn't work). The player detects collision with the box in the OnControllerColliderHit() and I know I can set damage here but I'm not sure if that's a good way to do it. \$\endgroup\$ – Richard David Jun 9 at 15:58
  • \$\begingroup\$ As I said, edit your question. Be sure to cover what the box represents and why it is kinematic, so we don't need to guess. \$\endgroup\$ – DMGregory Jun 9 at 16:00

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.