I’m don’t understand how it works. I want to use in if statement, when ball collision with player. How to use?
OnCollisionEnter is an event: Unity calls it when the object (which must have a Rigidbody) collides with any collider. This event may occur in both objects, the rigidbody and the hit object. In the player, the code could be like this:
void OnCollisionEnter(Collision col){
if (col.gameObject.tag == "Ball"){
// a rigidbody tagged as "Ball" hit the player
}
}
In the ball, the code is pretty much the same - the only difference is that the col structure contains info about the object hit:
void OnCollisionEnter(Collision col){
if (col.gameObject.tag == "Player"){
// this rigidbody hit the player
}
}
NOTE: Usually OnCollision events only occur when a Rigidbody hits a collider; when a simple collider (or CharacterController) hits a Rigidbody, this event will occur if and only if the Rigidbody isn’t sleeping - what means that it must be moving or spinning, since when the rigidbody properties velocity and angularVelocity fall below some limit the rigibody enters sleep mode.