OnCollisionEnter Issues

Well im going to be straightfoward here, this is my code

var collisionObject : Transform;


function OnCollisionEnter(collision : Collision)					//This function tests the collision and plays a sound
{
if (!audio.isPlaying && collision.gameObject == collisionObject)
{
audio.Play();
print("Play");
}
}

The “collisionObject” is the FPC and yes it has a rigidbody, the object the script is assigned to is a collision box with a Audio thing attached

I have no idea why it wont play…

Thanks Myhijim

collisionObject is a transform and collision.gameObject is a game object so they will never be equal! Perhaps define collisionObject as GameObject or check the transform of collision.gameObject.

So, this script is on a collider w/rigidbody and should be hit by the player, which uses a characterController?

CharControllers don’t trigger OnCollisionEnter when they hit things. They don’t use physics to move, which is where collisions happen. To test, add Debug.Log("hit by" + collision.transform.name); as the 1st line. You can verify it isn’t the math – there’s just no collision at all.

Instead need to use OnCharContHit. There are a bunch of threads here on that, and hacks that don’t really work (like adding a dummy isKinematic collider in front, which tends to just block the player’s movement.)