简体   繁体   English

Unity2D 检测多个触发碰撞

[英]Unity2D Detecting multiple trigger collisions

I'm trying to make an attack system to my game and for the moment is something like this:我正在尝试为我的游戏制作一个攻击系统,目前是这样的:

 private void Start()
{
    triggerActive = false;
}

private void Update()
{
    if (triggerActive)
    {
        if (Input.GetKeyDown(KeyCode.Space))
        {
            enemyOne.Attacked();
        }
    }
}

private void OnTriggerStay2D(Collider2D col)
{

    triggerActive = true;
   enemyOne = col.gameObject.GetComponent<Hittable>();
   
}
private void OnTriggerExit2D(Collider2D collision)
{
    triggerActive = false;
    enemyOne = null;
    
}

For the moment this works good, but now I want to try to detect more than one collider at the same so I can attack two different enemies at the same time.目前这很好用,但现在我想尝试同时检测多个对撞机,这样我就可以同时攻击两个不同的敌人。 How could I do that?我怎么能那样做?

You could check in OnTriggerStay2D:您可以检查 OnTriggerStay2D:

if (col.gameobject == enemyone)
...
else if (col.gameobject == enemytwo)
...

For enemyone and enemytwo you could use two public gameobjects that you reference externally in the Unity editor.对于敌人一个和敌人两个,您可以使用在 Unity 编辑器中从外部引用的两个公共游戏对象。

There are other examples how you can iterate over many enemies:还有其他示例如何迭代许多敌人:

void OnTriggerStay2D(Collision collision)
{
        foreach (ContactPoint contact in collision.contacts)
        {
            print(contact.thisCollider.name + " hit " + contact.otherCollider.name);
            // Visualize the contact point
            Debug.DrawRay(contact.point, contact.normal, Color.white);
        }
}

First you need move the trigger flag from this class to the Hittable class.首先,您需要将触发标志从此类移动到 Hittable 类。

class Hittable
{
    public bool isHit = false;
}

Then you need to replace enemyOne with a list of Hittable objects.然后你需要用一个 Hittable 对象列表替换敌人。

List<Hittable> enemies = new List<Hittable>();

When the trigger hits the enemy, we add the enemy into the enemy list.当触发器击中敌人时,我们将敌人添加到敌人列表中。 OnTriggerExit2D is not necessary, because we clear the list every frame. OnTriggerExit2D 不是必需的,因为我们每帧都清除列表。

private void OnTriggerStay2D(Collider2D col)
{
   var enemy = col.gameObject.GetComponent<Hittable>();
   if (!enemy.isHit)
   {
       enemy.isHit = true;
       enemies.Add(enemy);
   }
}

Finally we attack the enemies in the list every frame.最后,我们每帧攻击列表中的敌人。

private void Update()
{
    if (enemies.Count > 0)
    {
        if (Input.GetKeyDown(KeyCode.Space))
        {
            foreach (var enemy in enemies)
            {
                enemy.isHit = false;
                enemy.Attacked();
            }
        }
        enemies.Clear();
    }
}

You can use Physics2D.OverlapCircleAll to detect all the colliders (enemies) in a certain radius.您可以使用 Physics2D.OverlapCircleAll 来检测特定半径内的所有对撞机(敌人)。 Or you can also use Physics2D.CircleCastAll to detect all the colliders (enemies) except those are hidden by other ones.或者您也可以使用 Physics2D.CircleCastAll 来检测所有对撞机(敌人),除了那些被其他隐藏的碰撞机。

For more information, you should check the Unity Documentation.有关更多信息,您应该查看 Unity 文档。

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

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