Search
 
SCRIPT & CODE EXAMPLE
 
CODE EXAMPLE FOR CSHARP

detect collision in unity

/* Both objects have to have a Collider and one object has to have a Rigidbody for these Events to work */
private void OnCollisionEnter(Collision hit) {    
  Debug.Log(gameObject.name + " hits " + hit.gameObject.name); 
}
private void OnCollisionStay(Collision hit) {  
  Debug.Log(gameObject.name + " is hitting " + hit.gameObject.name); 
}
private void OnCollisionExit(Collision hit) { 
  Debug.Log(gameObject.name + " stopped hitting " + hit.gameObject.name); 
}
// For 2D
private void OnCollisionEnter2D(Collision2D hit) { }
private void OnCollisionStay2D(Collision2D hit) { }
private void OnCollisionExit2D(Collision2D hit) { }
 
PREVIOUS NEXT
Tagged: #detect #collision #unity
ADD COMMENT
Topic
Name
9+6 =