Why cant i reach my if statement?

im trying to get the bomb to explode the player if its dropped too low and hits the ground, when the player is flying along the ground.
why cant i reach the last if statement, even though the bool is true?

public Rigidbody2D rb;
    public Animator anim;
    private bool exploding = false;
    private bool hasHitGround = false;

    void Update()
    {
        if (exploding)
        {
            rb.velocity = Vector2.zero;
            rb.gravityScale = 0;
            FindObjectOfType<AudioManager>().Play("Explosion1");
            anim.SetBool("HitGround", true);
          
        }
    }

    void OnTriggerEnter2D(Collider2D col)
    {
      
        if (col.gameObject.tag == "GroundBorder")
        {
          
            hasHitGround = true;
            exploding = true;
        }
      
        if (col.gameObject.tag == "Player" && hasHitGround)
        {
            Debug.Log("has hit g = " + hasHitGround);
            Debug.Log("Plane destroyed by bomb on ground");
            exploding = true;
        }
    }
}

Well…You can only have one tag on an object. So, if the tag is “GroundBorder”…than it can’t be “Player” also.

2 Likes

if “hasHitGround” is true, and you’re sure OnTriggerEnter2D has been called, then that only leaves col.gameObject.tag does not actually equal “Player”. If it does equal “Player”, then either OnTriggerEnter2D isn’t being called or hasHitGround is not actually true. Add Debug.Log statements to output everything, including the tag.

But this is just it

without the && hasHitground the bomb explodes as soon as i press the bomb button proving that the collision is working
and if i do a Debug.Log just before the if, the bool is true…i just dont get it.

Add a debug.log before the if to check the tag of the thing that you are colliding with. If hasHitGround really is true, then that would suggest that there are no objects tagged “Player” that can you can collide with.

1 Like

ok

What exactly is this script attached to?

You set hasHitGround to true in the first if, so your first if is reached, that means, the tag of your gameobject is “GroundBorder” and as @Brathnann already said, your gameobject can NOT have two tags, so the second if will never be reached! Beside of that, you set exploding = true in the first if too, that´s the reason why it explodes, i guess.

1 Like

My guess is your second if was meant to be gameobject.tag and not col.gameobject.tag. Just a guess.

1 Like

I was thinking that, but it still seems weird. This script doesn’t seem written to be very general purpose. Like it is attached to just a specific object or specific kind of object. Usually in those cases you don’t ever need to check your own object’s tag, since you already know where the script is going. So I’m not really sure what he’s trying to do, unless he wants the first “if” to set the bool true on one collision, and then on a later collision he wants to actually blow it up. But his explanation of the issue doesn’t quite match.

2 Likes

My guess would be, that this script is added to the bomb object. That´s the only way i could explain the collision check with ground and player

2 Likes

yes sphinks you are right

“I was thinking that, but it still seems weird. This script doesn’t seem written to be very general purpose. Like it is attached to just a specific object or specific kind of object. Usually in those cases you don’t ever need to check your own object’s tag, since you already know where the script is going. So I’m not really sure what he’s trying to do, unless he wants the first “if” to set the bool true on one collision, and then on a later collision he wants to actually blow it up. But his explanation of the issue doesn’t quite match.”

im trying to get the bomb to explode when it first hits the ground, once its hit the ground if the player is there then it blows him up. hence the different checks.

The logic is not quite correct for what you want to do.

It can all be done in OnTriggerEnter2D and doesn’t need Update.

Setup the layers so that the bomb can only collide with what you want it to explode when it hits.
In OnTriggerEnter2D you cause the visual effect of the explosion to happen, then you do a physics query to get all the GameObjects within radius that can be damaged, then iterate over the GameObjects and apply damage and force to each one. Once done destroy the object.

" then you do a physics query to get all the GameObjects within radius that can be damaged, then iterate over the GameObjects and apply damage and force to each one. "

can you please elaborate on this…

Sure.

protected void OnTriggerEnter2D(Collider2D other)
{
    Collider2D[] results = new Collider2D[10];

    int resultCount = Physics2D.OverlapCircleNonAlloc(transform.position, blastRadius, results, layersThatCanBeExploded);

    float br = blastRadius * 2;

    for (int i = 0; i < resultCount; ++i)
    {
        GameObject go = results[i].gameObject;

        Vector2 dir = transform.position - results[i].transform.position;
        float dist = dir.sqrMagnitude;//Use this to calculate the damage/force, the closer to the center of the explosion the more damage/force.
        float t = 1 - (dist / br);//get the normalized distance and invert it.
        float dmg = blastDamage * t;

        //Do whatever here to damage the GameObject, use GetComponent to get the script attached that has health, 

        //Apply force
        if (results[i].attachedRigidbody)
        {
            dir.Normalize();
           
            results[i].attachedRigidbody.AddForce(dir * explosionForce * t);
         }
    }   
}

This is the idea of it, you might need to play around with damage and force to get it to work how you want it.

i redid everything with layers and its working great, no col.tag == etc, thanks for the example

for my bomb i just attached another object called afterShock which spawns another collider which only effects my plane…works great