- Home /
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
Answer by Owen-Reynolds · Jul 01, 2012 at 03:24 PM
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.)
Thanks, but what if i dont want it to stop the player, i phrased the question badly, i just want a collision box that sets off a noise as the player goes through it
A zone that simply detects your presence is a triggerBox. Check isTrigger
on the collider (it will no longer block anyone) and use the OnTriggerEnter
event. Also see the docs. Those do detect charControllers just fine.
Answer by whydoidoit · Jul 01, 2012 at 11:07 AM
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.
I suggest you Debug.Log both objects with .ToString(). They clearly aren't matching.
Your answer
Follow this Question
Related Questions
Any Idea why my Explosion sound is NOT playing ? 1 Answer
Can not get OnTriggerEnter and OnCollisionEnter to work!? 1 Answer
get collision info from main script? 1 Answer
First Person Controller Colliding Fail 0 Answers
Testing first person controller (mobile) and can't get the collisions to work 0 Answers