Abdou023
Abdou023

Reputation: 1664

Detect collision/colliding only once

I have an object that moves towards another object and physically collides with it, I want that collision/colliding event to happen only once. I tried using a bool but it didn't work as intended. It seems that I'm doing something wrong.

bool doDamage = true;
void OnCollisionEnter2D(Collision2D other)
{
    if (other.gameObject.tag == "Target" && doDamage)
    {
        doDamage = false;
        // damage code
    }
}

void OnCollisionExit2D(Collision2D other)
{
    if (other.gameObject.tag == "Target")
    {
        doDamage = true;
    }
}

Edit:

I want the "damage code" to run only once, even if the 2 objects are still in contact. This script is only assigned to 1 object, not both.

Upvotes: 1

Views: 7868

Answers (2)

CAD97
CAD97

Reputation: 1313

If you want to run a snippet of code once, run the code directly in the callback event that you want to trigger your code.

void OnCollisionEnter2D(Collision2D other)
{
    DoSomeDamageTo(other.gameObject);
}

This should only trigger once upon collision.

If you want this to only ever happen once (e.g. if the object holding the script were to hit something again), you'll need a flag to say that the damage has already been done:

bool hasHitSomething = false;
void OnCollisionEnter2D(Collision2D other)
{
    if (!hasHitSomething)
    {
        DoSomeDamageTo(other.gameObject);
        hasHitSomething = true;
    }
}

I suspect given the code you've shared, that either the objects are colliding multiple times in engine (very possible if they're solid and physics-controlled), leading to OnCollisionEnter being called multiple times. OnCollisionEnter should only be called once per collision. If you are observing it being called multiple times, either you are actually observing multiple collisions, or you've found an obscure Unity bug. The former is much, much more likely.

Upvotes: 0

Programmer
Programmer

Reputation: 125275

I don't know the easiest way to explain why your code is not working but I will try.

You have two GameObjects:

  1. GameObject A with doDamage variable.

  2. GameObject B with doDamage variable.

When GameObject A collides with GameObject B:

A.The OnCollisionEnter2D function is called on GameObject A. if(other.gameObject.tag == "Target" && doDamage) executes because doDamage is true.

B.The doDamage variable from GameObject A is then set to false.

This does not affect the doDamage variable from GameObject B.

Then

C.The OnCollisionEnter2D function is called on GameObject B. if(other.gameObject.tag == "Target" && doDamage) executes because doDamage is true.

D.The doDamage variable from GameObject B is then set to false.

Both your damage code will run because doDamage is always true in each OnCollisionEnter2D call. What you are currently doing is only affecting doDamage variable in each individual script.

What you are currently doing:

Setting doDamage in the local/this script to false while also checking if local/this doDamage is set or not.

What you need to do:

Set doDamage in the other script to false but read the local/this doDamage to check if it is set or not.

This is what it should look like:

public class DamageStatus : MonoBehaviour
{
    bool detectedBefore = false;

    void OnCollisionEnter2D(Collision2D other)
    {
        if (other.gameObject.CompareTag("Target"))
        {
            //Exit if we have already done some damage
            if (detectedBefore)
            {
                return;
            }

            //Set the other detectedBefore variable to true
            DamageStatus dmStat = other.gameObject.GetComponent<DamageStatus>();
            if (dmStat)
            {
                dmStat.detectedBefore = true;
            }

            // Put damage/or code to run once below

        }
    }

    void OnCollisionExit2D(Collision2D other)
    {
        if (other.gameObject.tag == "Target")
        {
            //Reset on exit?
            detectedBefore = false;
        }
    }
}

Upvotes: 2

Related Questions