Reputation: 298
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!
Upvotes: 0
Views: 49
Reputation: 2914
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;
}
}
Upvotes: 2