user1467534
user1467534

Reputation: 71

Unity - Collision detection within a collision detection

I am shooting a ball at blocks, and there are 3 Mega Blocks that make the weapon stronger. On my Mega Block 1, if a collision is detected from the Bullet Ball I shot, run code. That works fine. Now, if Mega Block 1 (tag=MegaBlock01) was hit, do one code, if Mega Block 2 was hit, etc. I wanted to put this code block on each Mega Block so I don't have to write new code for each.

public void OnTriggerEnter(Collider other)  // This code is on my Mega Blocks
{
    if (other.CompareTag("BulletBall"))  // The Ball has hit me
    {
       StartMegaWeapon01();

        // What I want is:  
        // If my tag is MegaBlock01, run StartMegaWeapon01();
        // If my tag is MegaBlock02, run StartMegaWeapon02();

    }
}

I want a collision detection within a collision detection.

Upvotes: 1

Views: 98

Answers (1)

Fotis Aronis
Fotis Aronis

Reputation: 78

You need to have the collider of your bullet checked as IsTrigger since you invoke the OnTriggerEnter method. At least one of the 2 colliding objects also needs to have a rigidbody so that the physics engine can properly compute collisions.

Once you have that, getting the tag of the gameObject from this should be enough:

if (gameObject.tag == "MegaBlock01")
    StartMegaWeapon01();

Upvotes: 2

Related Questions