简体   繁体   中英

Unity2D - Is there any function of detecting collision NOT enter?

I know there is function of detecting collision enter. That is OnCollisionEnter2D(){} but I want to know is there any function of detecting collision NOT enter? Or way to add else to OnCollisionEnter2D ?

Reason: I have two object and one variable HIT. If they touch each other the HIT variable set to 1. And if they NOT touch each other the HIT variable set to 0.

Is there any way to do that? This is my code:

void OnCollisionEnter2D(Collision2D coll){
    if (coll.gameObject == Collidor) {
        hit = 1;
    } else {
        hit = 0;
    }
}

The detecting of collision enter works well, but the else doesn't work.

Thank you!

Yes you can use OnCollisionExit2D to detect when the two touching Objects are no longer touching.

void OnCollisionEnter2D(Collision2D coll){
    if (coll.gameObject == Collidor) {
        hit = 1;
    } 
}

void OnCollisionExit2D(Collision2D coll){
    if (coll.gameObject == Collidor) {
        hit = 0;
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM