Is there a way two make two trigger gameObjects co

2019-07-15 20:21发布

This is for a 2D game.

I have a Player who can shoot trigger projectiles(with a trigger collider) and Enemies that can do the same. When a Player projectile collides with the Enemy, stuff happens and vice versa. However, when the Player projectile and the Enemy projectiles collide, they just ignore collision, go through each other, and nothing happens. They also have a Rigidbody2D with continuous collision detection.

Is there a way to make it so something happens when these two gameObjects with trigger colliders touch?

Here's what I've got for the Enemy projectile script:

void OnTriggerEnter2D( Collider2D other ){
    if (other.gameObject.name == "Ground"){ 
        Destroy (gameObject);
    } 
    else if (other.gameObject.name == "Player"){
        other.gameObject.GetComponent<RControlScript>().RHealth = other.gameObject.GetComponent<RControlScript>().RHealth - damage;
        Instantiate(transformInto, gameObject.transform.position, gameObject.transform.rotation);
        Destroy (gameObject);
    } 
    else if(other.gameObject.name == "Shot"){
        Destroy (gameObject);
    }
}

"Shot" being the name of the Player projectile being the gameObject not colliding with the Enemy projectile.

2条回答
仙女界的扛把子
2楼-- · 2019-07-15 20:56

Yes.

Here is a graph that tells you what collides with what in Unity3d.

enter image description here

查看更多
一夜七次
3楼-- · 2019-07-15 21:10

Ok, turns out two trigger colliders do in fact collide. My problem was that the projectiles instantiated were clones, therefore its name = "Shot(clone)". Had to change that in order to make things happen.

查看更多
登录 后发表回答