Collision detection not working... stumped!

Hi all,

My collision detection routine isn’t working and I can’t figure out why!
It’s attached to a first person character controller. Enemies are tagged ‘Enemy’ and have capsule colliders with ‘Trigger’ ticked… any ideas?

CollisionDetection.js:

#pragma strict
var AccessGUI : TheGUI;

function OnControllerColliderhit(hit : ControllerColliderHit)
{
	AccessGUI = gameObject.GetComponent(TheGUI);
	if (hit.gameObject.tag == "Enemy")
    {
    	Debug.Log("Hit");
    	AccessGUI.TakeDamage ();
	} 
}

I’m not even getting any debug messages in the console.

The TheGUI.js function, attached to the camera sub-object of the character controller:

#pragma strict

var ThePlayer : GameObject;
var TheEnemy : GameObject;
var TheEnemyAI : EnemyAI;
var EnemyDistance : float;
var EnemyLooking : boolean;
var EnemyAttacking : boolean;
var PlayerHealth = 100;
var PlayerX;
var PlayerZ;

function Update () 
{
 if(Input.GetKeyDown(KeyCode.Escape) == true)
 {
  Application.Quit();
 }
}

function OnGUI () {
ThePlayer = GameObject.Find("Player");
TheEnemy = GameObject.Find("Enemy");
TheEnemyAI = TheEnemy.GetComponentInChildren(EnemyAI);
EnemyDistance = TheEnemyAI.distance;
EnemyLooking = TheEnemyAI.isItLooking;
EnemyAttacking = TheEnemyAI.isItAttacking;
PlayerX = ThePlayer.transform.position.x;
PlayerZ = ThePlayer.transform.position.z;
GUI.Box(Rect(10,10,200,130),"Player X:"+PlayerX+"

Player Z:“+PlayerZ+”
Enemy Distance:“+EnemyDistance+”
Enemy Looking:“+EnemyLooking+”
Enemy Attacking:“+EnemyAttacking+”
Health:“+PlayerHealth+”

Escape to quit");
}

function TakeDamage () {
	PlayerHealth --;
}

Everything else in TheGui.js works.
Thanks in advance!

A collider marked as “IsTrigger” ignores physics, including collision calls. If you want collision turn off “IsTrigger” on the collider. Otherwise you need to use OnTriggerEnter ().

EDIT: The pics you posted were very helpful. In order to detect collision you have to have a RigidBody component on one of your objects. So, attach a RigidBody to your Enemy object and all should be right in the virtual world.

EDIT 2: Thanks for the project file. It helped out immensely. There are several thing that need to be done. The following outline those things:

THE COLLISION ISSUE:

I can’t believe I missed this: Fix the capitalization error in the OnControllerColliderHit() function. The “H” needs to be capitalized for this event to be called, otherwise, Unity will think you added a new custom function. I have made some additional changes to the CollisionDetection.js file below and commented those changes for you:

// CollisionDetection.js

#pragma strict    

var AccessGUI : TheGUI;    

function Start()
{

    // Assign the AccessGUI variable on initialization of
    // this script and cache it so that we don't have to do
    // a lookup everytime there is a collision.
    // Since we know the script is on a child object we
    // need to check in the children of this gameObject
    // Otherwise, we end up with null references later
    AccessGUI = gameObject.GetComponentInChildren(TheGUI);


    // Log the object that the TheGUI script was found on
    Debug.Log("TheGUI found on:" + AccessGUI.gameObject.name);

}    

// Correction made to capitalization of the "H" in
// this function name.
function OnControllerColliderHit(hit : ControllerColliderHit)
{   
    // If we collide with an "Untagged" object
    // exit so we aren't wasting time with the
    // floor we are standing on.
    if(hit.gameObject.tag == "Untagged")
        return;

    // Log the name object that was hit
    Debug.Log("The object hit was: " + hit.gameObject.name);

    // Log the tag of the object that was hit
    Debug.Log("The object hit has the tag: " + hit.gameObject.tag);

    // If the object is tagged "Enemy"  
    // We need to TakeDamage
    if (hit.gameObject.tag == "Enemy")
    {
        Debug.Log("Hit");
        AccessGUI.TakeDamage ();
    } 
}

To make it really easy, use the updated code above and place a cube in the scene with the tag “Enemy”. Run into the cube and behold the wonder. That way you can see the collision work without having to worry about your Enemy object.

OTHER ISSUES:

There are some issues with you enemy object and how it is set up. However, Answers is not the place for such in-depth discussion that is not directly related to the question you asked. So, I would be more than happy to work with you on those issues. Send me a PM on the forums and I will gladly discuss them with you so that you can get your game mechanics working as you expect.