简体   繁体   English

如何判断哪个对撞机发生了碰撞?

[英]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. 我正在创建一个有敌人的游戏,我希望游戏中有爆头,所以我有2个对撞机:一个撞到头部,一个撞到身体。 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. 您需要创建BoxCollider2D类型的公共变量并分配对撞机。 When a collision occurs call an IF statement inside of OnTriggerEnter to see which one has collided. 发生冲突时,请在OnTriggerEnter内部调用IF语句,以查看发生冲突的那条 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. isTouching是一个Unity方法,该方法根据正在比较的对撞机返回布尔值。

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 ;) ) 我建议不要在同一个GameObject上添加所有对撞机,而应为每个对撞机赋予它自己的子GameObject (这样,您还可以在场景视图中轻松查看哪些对撞机属于哪个轮廓;))

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;
        }
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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