0
votes

Hi guys I am having a problem in unity with OnTriggerEnter2D.

The player has 2 bullets, any time the player bullet hits the enemy it should increment the score by 100, but for some reason it is incrementing the score by 200 if two player bullets hit the enemy. Both bullets fire at the same time and are at either side of the players gun. So it should only ever increment the score by 100 and not 200.

Here is my code:

void OnTriggerEnter2D(Collider2D col)
{
    if(col.tag == "PlayerBullet")
    {
        this.score += 100;
    }
}

Other information:

My player bullet and enemy both have Box Collider and RigidBody2D attached to them. They both have the isTrigger option checked.

4
If each bullet is worth 100 and "is incrementing the score by 200 if two player bullets hit the enemy" it sounds like it's working to me.. Maybe you need to clarify the question? - A Friend
Sorry, it should only increment the score by 100, regardless of if one or two bullets touch the enemy. - Blaze
Just to be clear. If the player shoots any number of bullets at the enemy, and let's for simplicity assume they all hit, you only want to total up 1 hit? I just want to avoid you coming back and saying things like "OK, so the player shoots 2 bullets, I only want to register 1 hit, but if he later on shoots the same enemy again, I want to count that as well". Because then we need more criteria. - Lasse V. Karlsen
I don't get you logic. You said there are two bullets and so everytime a single bullet hit the player, it will increment by 100. So it will be 200 for two bullets. - Saad Anees
I only want to register one collision. Please let me know if you understand what I mean now, extremely sorry for the confusion! - Blaze

4 Answers

2
votes

If you want it to work just once you can ignore collision after first increment of the score(aka after first collision) like this:

void OnTriggerEnter2D(Collider2D col)
{
   if(col.tag == "PlayerBullet")
   {
      this.score += 100;
      Physics2D.IgnoreCollision(col, GetComponent<Collider2D>())
   }
}

Note that After this point all collisions between PlayerBullet and Enemy will be ignored.

0
votes

If I understand it correctly, the player has two guns, and I assume they are fired at the same time.

In this case, we can make it more beautiful with condition-condition statements.

I'm leaving the code block below.

    if (Input.GetMouseButtonDown(0) && Input.GetMouseButtonDown(1))
    {
        Debug.Log("Fire Metod(Score)");
    }
    else
    {
        if (Input.GetMouseButtonDown(0))
        {
            Debug.Log("Fire Metod(Score)");
        }

        if (Input.GetMouseButtonDown(1))
        {
            Debug.Log("Fire Metod(Score)");
        }
    }
0
votes

Often times you can set a flag to tell the game to only increment once. A flag is just a bool value, and can be private within the scope of the class. Sort of like:

if(canHit)
{
   canHit = false;
   //add score
   canHit = true;
}

That basic formula should get you the results you want.

0
votes

I'm upgrading my comment to an answer because after reading your question and the comment responses I believe you need a non-code answer.

You want two objects to work together and count as one 'hit' when they collide with another object. That sounds to me like a situation where you should be placing those objects inside another one, let's call it a 'shot' object, and you should be doing collisions and whatnot based on that. That way, if both bullets hit only one 'shot' hit, so it will count with the code as-is and it sounds like it will be implemented more as you've expected it to be.