unity collision detection code example
Example 1: unity how to tell when a gameobject is colliding
void OnCollisionEnter(Collision collision){
if (collision.gameObject.name == "MyGameObjectName")
{
Debug.Log("Do something here");
}
if (collision.gameObject.tag == "MyGameObjectTag")
{
Debug.Log("Do something else here");
}
}
Example 2: unity how to make two objects not collide
void OnCollisionEnter(Collision collision)
{
if (collision.gameObject.tag == "theobjectToIgnore")
{
Physics.IgnoreCollision(theobjectToIgnore.collider, collider);
}
Example 3: check for collision unity c#
void OnCollisionEnter(Collision col) { }