- Home /
Set ContactFilter2d to not use child object's triggers.
In our player's GameObject, we have a collision box to track the player's collision, as well as a trigger in a child object that activates when using an attack. To get the movement we wanted, we followed Unity's 2d platformer physics tutorial which can be found here: https://unity3d.com/learn/tutorials/topics/2d-game-creation/intro-and-session-goals?playlist=17093
Everything works fine until we enable the child's trigger. After some degugging, I believe that the ContactFilter2D is using the child object's trigger as a collider as well. Is there any way to get around this, or will I have to place the triggers in a object that is not a child of the player?
Answer by OmegaJeff · Jul 17, 2017 at 11:20 PM
This is definitely a Unity bug with both the ContactFilter2d and RigidBody2d.Cast(). The main issue seems to be that RigidBody2D.Cast() is including the child triggers as part of the object's overall collider shape, and there is no override option in the function itself to tell it not to do that.
We managed a two step work around, but it isn't perfect and we won't be relying on Rigidbody2d.Cast() for anything else until it's fixed.
The first step is to manually set the LayerMask for the filter to exclude any layers you're using for triggers.
The second step is to have the actual trigger collider and any OnEnter/Stay/Exit scripts stored in an object outside the rigibody's hierarchy, then use a placeholder child in the hierarchy. Set up a simple script on a child of the rigidbody to pass its transform values (and any other values like enable, offset, size, etc...) to the external object. If you're using scale flipping, be mindful to offset the position by the reference object's scaled local position.
Reassuring to hear that its a Unity bug and not just us using ContactFilter2D incorrectly. We have been working at the problem as well, and are pretty much on the same path you described. It's a bit frustrating that we have to handle it this way, but for now it's our only real option as long as this bug exists. Thanks for the answer!
This is the first I've heard of this and no bug has been submitted either. This is why bugs are not fixed; because many people leave it up to others. I'll check it out but I'd also appreciate a bug report if possible.
Hello. I actually did submit a bug report for this, around the same time I wrote this initial workaround back in July 2017. The case number was 932584, and this is the developer response I was forwarded via support:
Rigidbody2D.Cast()
will sweep all attached Collider2D
on the Rigidbody2D
so yes, it's by design. That said, for some users who want to ignore any attached Collider2D
set to be triggers, we should probably add that as an option so this is more a feature request and not a bug. The ContactFilter2D
option (mentioned in the case) set to not use triggers refers to Collider2D
being detected, not the Collider2D
being swept i.e. should we detect triggers or not.
Note that Rigidbody2D.Cast()
is simply a convenience function. The user can easily use the following features to grab all the attached colliders: https://docs.unity3d.com/ScriptReference/Rigidbody2D-attachedColliderCount.html https://docs.unity3d.com/ScriptReference/Rigidbody2D.GetAttachedColliders.html
They can then iterate them perfor$$anonymous$$g Collider2D.Cast on each, ignoring any that are triggers: https://docs.unity3d.com/ScriptReference/Collider2D.Cast.html
Thank you for looking into this.
Thanks! So I follow now, afraid I read your problem wrong above. I thought you meant that the Rigidbody2D.Cast was causing colliders attached to that body to contact other (trigger) colliders on the same body, something which is explicitly checked for. $$anonymous$$y bad.
I see now that you meant not using trigger colliders on that body as part of the sweep. I do recall that case and it was me who gave that message to the QA to reply. On our internally planning board there is a card amongst many small Rigidbody2D features, this is one but it's not yet been implemented, effectively providing an option to control whether triggers should be used or not as part of the sweep.
I've got a list of 9 low-hanging fruit items to be implemented in the next few months, I'll make a promise here to add that to the list. Sorry for the inconvenience.
I believe the workaround above though pretty much does what Unity will do anyway i.e. iterate all colliders on the body, ignore any triggers and perform a Collider2D.Cast on each and use all the results or only the closest depending on what you want.
Answer by SebaLagos · Apr 06, 2018 at 05:30 PM
Is this fixed or still happening @OmegaJeff ? we are on the 2017.4.0f1 and still happening, even with the manual setup of the layermask
Looking at the documentation, I don't think they've made a change but I haven't tested it.
We did find a quicker/easier/less complicated workaround though.
Create a reference to your Collider2d in the script, and then use Collider2d.Cast() ins$$anonymous$$d of Rigidbody2d.Cast(). It will cast only the referenced Collider2d, and it wont return hits against any children of the Rigidbody2d. It's a bit cleaner if you want to keep the Rigidbody2d at the top of the prefab's hierarchy.