- Home /
Triggers colliding when they are set not to in Collision Matrix
I have 2 objects. Player and Tower set to layers Player and Armory respectively. Both have rigidbodies. Each one has a child called Vision which is a sphere collider set to trigger and both visions are in a layer called Trigger. In the Physics inspector, I set the collision matrix NOT to collide between Trigger/Trigger but collide between Player/Trigger, Armory/Trigger.
So, I assume that those Vision triggers are not going to collide with each other. But they do. I get OnTriggerEnter/Stay/Exit between these 2 colliders which are in the same layer Trigger. Which is set not to collide.
Is the physics engine pretending that the Vision child of Player to be in the same layer of Player? And the same for the Vision child of Tower?
Is there a way to get around this?
Thanks in advance! Cheers!
Answer by RyanZimmerman87 · Mar 09, 2013 at 03:19 AM
So far I haven't even used trigger functions whatsoever. Perhaps they will save you time if you use them properly but in my experience so far they are not necessary.
If you are having trouble getting them to work properly you could try something like this:
void OnCollisionEnter(Collision theCollision)
{
//get the script for the object which you are checking collisions for example:
ArmoryScript otherObjectsScriptArmory = theCollision.gameObject.GetComponent<ArmoryScript>();
if (otherObjectsScriptArmory != null) //the script IS on that game object
{
//your desired behavior
}
}
Using something like that has allowed me to detect all collisions between objects as expected. In this example you would put this in your Player's script to detect if a collision occurs with a game object containing your Armory Script.
This method allows you to check for as many different collision types as you want all within one function.
Triggers may actually be the superior method but right now I prefer this.
This example is assuming the script attached to your tower is called ArmoryScript. You will need to adjust all the names to suit your project.
As far as I can tell, a script attached to a trigger will not get the 3 OnCollision events. It will get the 3 OnTrigger events. I don't know if I want to check the components like that either. That can be fine if you have a small number of component types to check against. But if there are a bunch, this can be very costly, especially for mobile.
I agree the downside to this method is that it could be costly on performance. It seems like checking 3+ different components on my current Android project is not causing issues yet though.
I think if you use:
return;
within the different object checks and start with the most likely colliders it won't be to bad.
I just like this method because it's very easy to set up. I am curious how much of a performance difference it actually is with the trigger events.
Answer by Filippopotamus · Mar 09, 2013 at 03:49 AM
I not familiar with the internals, but I would assume trigger would be quicker since they are mostly ignored by the physics engine since they don't react to each other.
Answer by Loius · Mar 09, 2013 at 03:54 AM
Debug.Log your objects' layers and names in your trigger functions, to be sure everything's in the layer you expect.
You could also try making your player and its vision object children of a new, default-layer object, and see if that brings up collision issues.
If your player has a rigidbody/Controller, and your vision does not, AND your vision is a child of the player, it's possible that the entire collision tree is being set to the same layer as the rigidbody. The fix to that should be as simple as adding a kinematic rigidbody to each vision objects.
Answer by MrMeows · Jun 11, 2015 at 09:01 AM
I have the same problem. It appears that triggers ignore the layer collision matrix. For your problem, I would do something like this:
void OnTriggerStay(Collider NewCollider)
{
if(NewCollider.gameObject.layer != 10)
{
Function();
}
}
...assuming that 10 is the layer index of your Vision GameObjects.
P.P. I just noticed when this was asked. Oops.
Your answer
Follow this Question
Related Questions
Child object's collider (on a different layer) is interfering with parent Physics... 0 Answers
Collider trigger only affecting a single game object when it should affect multiple 0 Answers
Layers, parenting and collisions. 0 Answers
How having correct bounce on a rotating pinwheel(it's more complicated)? 0 Answers
How do I stop the trigger effects once the player leaves the collider? 2 Answers