0
\$\begingroup\$

I tried with this:

void OnCollisionEnter(Collision col)
{
    Debug.Log("hit everything");
    if (col.gameObject.layer != LayerMask.NameToLayer("bico"))
    {
        if (col.gameObject.tag == "Floor")
        {
            Debug.Log("hit");
            Destroy(gameObject);
            ShipHealth ShipHealth = this.GetComponentInParent<ShipHealth>();
            if (ShipHealth != null)
            {
                Destroy(gameObject);
                ShipHealth.TakeDamageMissile(100);
            }
        }
    }
}

What I want is that it only chooses one collider and not all: I only want the capsule collider.

\$\endgroup\$
  • \$\begingroup\$ Its not clear what you want. Do you want to destroy the capsule collider when it hits the floor? Where is the code you provided running? On the whole ship or just the capsule collider objcet ? \$\endgroup\$ – Uri Popov May 30 '16 at 11:31
2
\$\begingroup\$

I'm a little uncertain about your question, but I believe you are asking how to determine which collider triggered the collision so that you can perform distinct actions.

I can think of two ways to do this. First, if the capsule collider area (let's call it the bridge) and the box collider area (the hull) have a lot of distinct functionality, it may be wise to have separate MonoBehavior scripts handling collisions for each. For example, consider this game object hierarchy in Unity:

Ship (Empty parent)

  • Bridge (Capsule collider and BridgeSystem script)
  • Hull (Box collider and HullSystem script)

BridgeSystem would have an OnCollisionEnter method that only detects the capsule collider, and HullSystem would have its own OnCollisionEnter method for the box collider. If you want additional collision detection for both, I believe you could also attach a MonoBehavior to the Ship parent, which would receive all collisions. Alternatively, the bridge and hull collisions could call a public method in the parent.

If you really want to keep a single script that distinguishes between the two colliders, you should be able to use Collision.ContactPoint. For example, col.contacts[0].thisCollider should allow you distinguish between the box and the capsule.

| improve this answer | |
\$\endgroup\$

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.