void OnControllerColliderHit(ControllerColliderHit hit) { if (hit.gameObject.tag == "Enemy") { // Damage the enemy EnemyController enemy = hit.gameObject.GetComponent(); enemy.TakeDamage(damageAmount); } }
void OnControllerColliderHit(ControllerColliderHit hit) { if (hit.gameObject.tag == "Collectible") { // Collect the item Destroy(hit.gameObject); inventory.AddItem(hit.gameObject.name); } }In this example, we are detecting when the character controller collides with a collectible object. We then destroy the object using the Destroy function and add its name to the player's inventory. Both of these examples are using the ControllerColliderHit class to detect collisions and react accordingly. These code examples can be found in the UnityEngine package library in the Unity editor.