usingUnityengine;usingSystem.Collections; Public classHitcheck:monobehaviour {///two objects can collide with the condition that two are all with collider and one of them is a rigid body. //Use this for initialization voidStart () {}//Update is called once per frame voidUpdate () {}/////////////////Trigger//////////////////// //just entering the trigger range. voidOntriggerenter (Collider other) {print ("Enter trigger range"); } //stays within the trigger range voidOntriggerstay (Collider other) {print ("Continuous triggering"); } voidOntriggerexit (Collider other) {print ("leave the trigger range"); }//////////////////collision-related three functions//////////////////// //the two objects that collide must have relative motion//It is called once at the beginning of the collision and "collision with ground will also trigger" voidOncollisionenter (Collision other) {print ("Collision Start"); } //have contact, and have relative movement voidOncollisionstay (Collision other) {print (other.gameObject.name); if(string. Equals ("Cube2", Other.gameObject.name)) {Print ("There was a collision with Cube2."); } Print ("in Collision"); } //End of collision voidOncollisionexit (Collision other) {print ("End of collision"); } ///////////////////////////////////////////}
Unity3d Collision Detection