简体   繁体   中英

How to tell which of the colliders has been collided?

I'm creating a game in which there are enemies, I want to have headshots in the game so I have 2 colliders: one to the head and one to the body. I can't find any good way to tell which is which in the code.

I thought of a solution but I don't like it- a different type of collider to the head, and different type to the body (like polygon and box colliders). It works but I don't think it's good enough (if I want to add more colliders or have two of the same type that wouldn't work).

virtual protected void OnTriggerEnter2D(Collider2D collider2D)
    {
        if (collider2D.gameObject.tag.Equals("Zombie"))
        {
            Destroy(gameObject);//destroy bullet
            Zombie zombie = collider2D.gameObject.GetComponent<Zombie>();
            if (collider2D is BoxCollider2D)
                zombie.HeadShot(demage);//headshot
            else zombie.BulletHit(demage);//normal hit
        }
    }

I want a way to tag the colliders somehow so I can tell between them.

You need to create public variables of type BoxCollider2D and assign your colliders. When a collision occurs call an IF statement inside of OnTriggerEnter to see which one has collided. This will work no matter if there are more of the same types of collider.

public class Example : MonoBehaviour
{
    public BoxCollider2D box01;
    public BoxCollider2D box02;

    private void OnTriggerEnter2D(Collider2D collision)
    {
        if(collision.IsTouching(box01))
        {
            Debug.Log("1");
        }
        else if(collision.IsTouching(box02))
        {
            Debug.Log("2");
        }
    }
}

isTouching is a Unity method which returns a bool depending on a collider that is comparing.

I would suggest to not add all colliders on the same GameObject but rather give each collider it's own child GameObject (this way you also can see easily which colliders belongs to which outline in the scene view ;) )

Then you could use a class with an enum to define which type of collider you have there:

public class BodyPart : MonoBehaviour
{
    public BodyPartType Type;
}

public enum BodyPartType
{
    Head,
    LeftArm,
    RightArm,
    Body,
    LeftLeg,
    RightLeg
}

and attach it to all body parts next to each collider.

Then you could do something like

virtual protected void OnTriggerEnter2D(Collider2D collider2D)
{
    if (collider2D.gameObject.tag.Equals("Zombie"))
    {
        Destroy(gameObject);//destroy bullet

        // Note you then should use GetComponentInParent here
        // since your colliders are now on child objects
        Zombie zombie = collider2D.gameObject.GetComponentInParent<Zombie>();

        var bodyPart = collider2D.GetComponent<BodyPart>();
        switch(bodyPart.Type)
        {
            case BodyPartType.Head:
                zombie.HeadShot(demage);//headshot
                break;

            // you now could differ between more types here

            default:
                zombie.BulletHit(demage);//normal hit
                break;
        }
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM