- Home /
chronological order of collision to give a winner unity 2d 4.3
i am developping a 2d game on unity 4.3 , i need to make 2 specific collision before giving a certain output based on those collision , how can i do that ?
Comment
Answer by sanmn19 · Jan 12, 2014 at 03:06 PM
You probably try this script(C#): Set object1Name and object2Name to your desired Object names, where object1Name is supposed to collide first followed by object2Name, in Start() method. The threshold and invoke makes sure that object2 has to touch this gameobject within 'secondObjectCollideThreshold' seconds(3 in my code) to trigger your desired event.
bool hasCollidedWithObject1;
string object1Name;
string object2Name;
float secondObjectCollideThreshold;
void Start(){
object1Name = "MyObject1";
object2Name = "MyObject2";
secondObjectCollideThreshold = 3.0f;
}
void OnCollisionEnter(Collision collision){
if( collision.gameObject.name.Equals( object1Name ) ){
hasCollidedWithObject1 = true;
CancelInvoke();
Invoke("forgetObject1Collision", secondObjectCollideThreshold );
} else if( collision.gameObject.name.Equals( object2Name ) ){
CancelInvoke();
if( hasCollidedWithObject1 ){
hasCollidedWithObject1 = false;
//Do your desired event here
}
}
}
void forgetObject1Collision(){
hasCollidedWithObject1 = false;
}