Need help with collision checking

This is gonna be quite beefy so I’ll put it into sections.

GOAL: I’m trying to set up a simple (weapon collides with enemy > enemy takes damage) script.

PROBLEM: After trying to debug my code and reading tons of things over the years about colliders, triggers, and rigidbodies, I’m starting to get confused about what is what.

Like which of the two objects need a Rigidbody based on whether I’m using “OnTriggerEnter” or “OnCollisionEnter”, as well as which of the two objects are supposed to be triggers. Also things like; Does “OnTriggerEnter” detect whether something enters this object’s trigger, or whether the trigger of this object enters something else, and same with “OnCollisionEnter”.

SETUP: The script is on the enemy object, and it’s looking for an object with the “Weapon” tag colliding with it. The enemy has to have a non-trigger collider and a kinematic rigidbody, because I don’t want the player to be able to pass through the enemy and also not be able to move them around. The sword object also has to have a trigger collider because I don’t want it physically colliding with neither the enemy nor the player.

What would be the correct method to use, OnCollision or OnTrigger? And would the sword also need a rigidbody?

Refer to the Colliders Overview section in the manual. It’s complicated but that page gives you all the necessary information.

Generally, colliders are used when things are supposed to collide physically. Triggers, on the other hand, only the detect overlaps and won’t prevent colliders from going through.

You’ll only get OnCollision* messages from two colliders actually colliding (that’s why these messages give you a Collision objects containing information about how exactly the objects collide). If either or both of them are triggers, you’ll only get the OnTrigger* message (since there won’t be an actual collision).

The rules for when objects actually trigger message or simply ignore each other are complicated and also depend on the Contact Pairs Mode set in the physics project settings. Refer to the tables at the bottom of the colliders overview page but note that it does not reflect the contact pairs setting.

1 Like