1

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.

1 Answer 1

2

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();
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.