4
votes

I have a gameobject with two sphere colliders attached. One has IsTrigger checked and the other not.

I want to execute different set of statements when collision occurs with different colliders. For example I want to play different sound for both different collisions. Is there any way to achieve it?

I tried OnTriggerEnter() but unfortunately it is called for both type of collisions since other colliding objects have triggered colliders. I just thought if we could somehow find out on which collider of the gameobject the collision has taken place we will be able to achieve it.

So is there any way to get through with this?

3
That's strange, if only one is checked as IsTrigger only that should call OnTriggerEnter. Have you tried using two child GameObject one for each SphereCollider?pinckerman
OnTriggerEnter worked for both colliders as i observed.Sanath Bharadwaj
I almost got it. I think using child gameobject is a cool idea.Sanath Bharadwaj

3 Answers

1
votes

I have been using Unity for years and faced tons of problems like this, related to bad software design. I hope Unity guys will handle physics more carefully in future releases.

In the mean time, you can use Physics.OverlapSphere and Physics.CheckSphere to manually check if there is something that collides with your object. Remove the collider that you are using as a trigger and use these methods instead of OnTriggerEnter. This is a bit hacky, but this will do the job I think.

1
votes

Make your colliders visible in the inspector (make them public or add [SerializeField] before it) and then tie in the colliders to the code that way.

Then, in your collisions, compare the colliding objects against your variables that are holding the colliders for you to keep them separate.

0
votes

To detect for source trigger in OnTriggerEnter, you must use workaround with multiple gameobject hosting trigger and satellite scripts.

Allow me to link to my answer on gamedev SO: https://gamedev.stackexchange.com/a/185095/54452