简体   繁体   English

Unity,BoxCollider2D,碰撞但不触发

[英]Unity, BoxCollider2D, Colliding but not trigerring

I have 3 gameObjects: "Player" (with BoxCollider2D) Weapon with (script attached, BoxCollider2D(isTrigger = true)) and "Enemy" with (BoxCollider2D, and tag "Enemy") I put a script to Destroy the enemy upon weapon to Enemy collison but it just goes past enemy until the player collider pusher the enemy.我有 3 个游戏对象:“玩家”(使用 BoxCollider2D)武器(附加脚本,BoxCollider2D(isTrigger = true))和“敌人”(BoxCollider2D,并标记“敌人”)我放了一个脚本来摧毁武器上的敌人敌人碰撞,但它只是越过敌人,直到玩家对撞机推动敌人。

{   
    private void onTriggerEnter2D(Collider2D collision)
    {
        if (collision.gameObject.CompareTag("Enemy"))
        {
            Destroy(collision.gameObject);
        }
    }
}```
    

That might be becuase you are using a collider, not a trigger.这可能是因为您使用的是对撞机,而不是触发器。 Try use OnCollisionEnter2D instead of OnTriggerEnter .尝试使用OnCollisionEnter2D而不是OnTriggerEnter

private void OnCollisionEnter2D(Collision2D collision)
{
    if (collision.gameObject.CompareTag("Enemy"))
    {
        Destroy(collision.gameObject);
    }
}

If you don't get any errors it looks like your handler method has invalid name in this context.如果您没有收到任何错误,那么您的处理程序方法在此上下文中的名称似乎无效。

Change onTriggerEnter2D to OnTriggerEnter2D将 onTriggerEnter2D 更改为 OnTriggerEnter2D

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

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