x


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

more ▼

asked Jul 01 '12 at 11:05 AM

Myhijim gravatar image

Myhijim
25 1 6 9

(comments are locked)
10|3000 characters needed characters left

2 answers: sort voted first

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.)

more ▼

answered Jul 01 '12 at 03:24 PM

Owen Reynolds gravatar image

Owen Reynolds
11.3k 1 7 45

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

Jul 02 '12 at 03:03 AM Myhijim

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.

Jul 02 '12 at 05:34 AM Owen Reynolds

Exactly what i needed! Thank you

Jul 05 '12 at 11:51 AM Myhijim
(comments are locked)
10|3000 characters needed characters left

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.

more ▼

answered Jul 01 '12 at 11:07 AM

whydoidoit gravatar image

whydoidoit
33k 11 23 100

Nope that didnt work :( Thanks anyway

Jul 01 '12 at 12:03 PM Myhijim

I suggest you Debug.Log both objects with .ToString(). They clearly aren't matching.

Jul 01 '12 at 12:10 PM whydoidoit
(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x1027
x227
x124
x64

asked: Jul 01 '12 at 11:05 AM

Seen: 416 times

Last Updated: Jul 05 '12 at 11:51 AM