Having OnCollisionEnter/Exit confusion in my code

I’m having a real confusing problem with collisions that I’m sure has a simple solution. So I have a box in the world with these collison properties:

I also have a player in the world who has collision settings like this:

They look like this in the game (the player has a projector and an audio source attached, that’s why it looks odd):

The box has a box collider that exactly fits the mesh shape.

For testing purposes, I threw this C# script onto the box:

using UnityEngine;
using System.Collections;

public class TestScript : MonoBehaviour
{
	void OnCollisionEnter( Collision _other )
	{
		Debug.Log( "ENTER" );
	}

	void OnCollisionExit( Collision _other )
	{
		Debug.Log( "EXIT" );
	}
}

Now, if I walk across the box or jump on/off of it, all I see are “EXIT” lines appearing in the log. I never see the “ENTER” notification. Ever.

Is there anything I should be looking at here that comes to mind?

Thanks!

What you want maybe is a OnControllerColliderHit() on your player script. Collision with non rigidbody objects is always wierd.

Unity Docs link : file://localhost/Applications/Unity/Unity.app/Contents/Documentation/Documentation/ScriptReference/CharacterController.html
(might not work)

That seems to register the collision reliably so I’ll see what I can do with that. Thanks!

So RigidBody collision is flaky then? That sounds somewhat scary for making games. :?

As far as i know, a rigid body collision works perfectly, when the rigid body moves at speeds equal or below is its own z-dimenson per frame.

So, for example a rigid body with z = 1 meter should not move more than 1 meter per frame. If you have rigid bodies moving faster you can decrease the time step in the physics setting to keep collisons acurate.

This one might work better.
http://unity3d.com/Documentation/ScriptReference/CharacterController.html

The character controller doesn’t use physics and does therefore not have a RigidBody.

OK, but what is he referring to then when he says:

“Collision with non rigidbody objects is always wierd.”

Is there something weird there that I should be aware of going forward?

No there is nothing weird… except that you need to have a rigidbody on least one of the colliding objects to register collisions.

OK, so I think my question boils down to this:

What is the best way to script an object so it knows that a charactercontroller has come into contact with it?

I know I can capture collisions within the charactercontroller (CC) itself via “OnControllerColliderHit” but that’s somewhat limiting as there doesn’t seem to be a companion function to tell me when the CC has stopped touching something. This is needed so I can know when, for example, the player has stepped off of a lift.

It feels like the CC is a cool concept and makes the basics very easy, but it is fairly limited when it comes to physical interactions with the world (i.e. collisions and notifications of such). Is this a fair assessment?

What I meant was objects without rigidbodies should not be relied on to get collision messages. In this case a script that checks if the player is inside a child trigger.

if(myTrigger.bounds.Contains(player.trasform.position))
{
    print("player on lift");
}

Would be best, probably.

Thanks Yoggy. I’ve set it up now with a trigger attached to the top of the lift so I get proper notifications of the players comings and goings. It isn’t ideal (I strive for utmost simplicity when I can get it) but it’ll work.