thielr7
thielr7

Reputation: 337

Unity C# capsule collider and rigid body don't trigger using transform.position to move

I have 2 simple capsules. 1 is stationary and the other is moving to the same tile on the tilemap using transform.position.

Both capsules have capsule colliders and rigid bodies. I've attempted to remove the rigid body but from what I can tell, the OnCollisionEnter function requires a rigid body to work.

My script, attached to both of these, is a simple:

private void OnCollisionEnter(Collision other)
{
    print("Collision Detected! " + other.gameObject.name);
}

I've used combinations of 'isKinematic', 'isTrigger', placing one component before the other then reversing, ensuring I have the script attached to both objects, making sure my capsule collider is sized to the object, and looked at other people's issues to try their steps to fix my issue.

Nothing seems to trigger the collision between the 2 capsules. It's got to be something small I'm missing.

I have a tag on both objects and have tried things like if(other.gameObject.tag == "myTag") {...} but that's not working for me, either.

Can anyone spot my mistake? Let me know if you need to see anything else - happy to provide images or whatever will help. Thanks in advance!

My Hierarchy

Capsule Mesh Components

Above is the basic identical hierarchy of both capsules with components attached to the capsule mesh. Both have the parent empty objects they're stationed under. (Which themselves, contain scripts but no collider or rigid body)

Upvotes: 0

Views: 1193

Answers (2)

Gullie667
Gullie667

Reputation: 135

Make sure you are not looking for

void OnTriggerEnter(Collision collision){}

Upvotes: 0

TEEBQNE
TEEBQNE

Reputation: 6266

Here is an infographic to show when a collision message will be detected by OnCollisionEnter between two objects. Both objects will need some sort of collider, and will most likely need a Rigidbody.

Collision Matrix

You will not want to set isTrigger as that will not make it physically react to a collision, but will just detect when a collision occurs. It will also not call OnCollisionEnter but will call OnTriggerEnter. Setting both not as triggers, adding a collider, and giving them Rigidbodies should allow the collision to be detected. You will also need to attach this script to one of the objects that have the collider. Are there other components on the objects you are using?

Upvotes: 1

Related Questions