Coote
Coote

Reputation: 1

Unity OnCollisionEnter with a collider that is the child of the rigidbody

I am attempting to only have the collision continue when contacting the Player (the parent with the rigidbody), directly; and ignoring collisions with the child (a sword). the sword is tagged weapon, and the player with player.

I have searched, and cannot find a sufficient answer (C#)

void OnCollisionEnter (Collision col){
    Debug.Log("boop P" + playerNumber);
    if (col.collider.transform.tag == "Player") { 
        -stuff happens-
    }
}

This is driving me crazy and I need sleep, please help.

Edit - I solved it after ages, with a simple thing called ContactPoint.otherCollider

Upvotes: 0

Views: 1961

Answers (2)

Lucky
Lucky

Reputation: 4493

In case anyone else is struggling with this and OP's edit was too vague...

You can check to see the colliders that were hit on the GameObject calling the script using collision.contacts:

foreach (ContactPoint c in collision.contacts)
{
        Debug.Log(c.thisCollider.name);
}

Upvotes: 1

mikemsrk
mikemsrk

Reputation: 26

The problem may be how you're checking for the tag. I usually grab the gameObject's tag directly like so.

void OnCollisionEnter(Collision col){
    if (col.gameObject.tag == "Player"){
        //stuff happens
    }
}

Or even the collider's tag.

(col.tag === "player")

Upvotes: 0

Related Questions