- Home /
Two colliders on GameObject with only one acting as trigger
I'm making a 2D Android game and I need two colliders on each object.
One is a box collider that's a little bigger than the sprite to act as a hit box and detect whether the user has touched the object. It needs to be a little bigger than the sprite so that the touch will still be recognised if the user touches a little bit outside the object.
The other is a polygon collider that matches the shape of the sprite - I want this to act as a trigger to use with the OnTriggerEnter method.
The problem is that both colliders are acting as a trigger and activating the OnTriggerEnter method, which means the method is running when another object comes close to the object since the Box Collider is bigger than the sprite.
I've unticked the 'Is Trigger' box on the Box Collider but it hasn't helped.
Is there a way to have two colliders on on object but only have one act as a trigger? Does anyone have any other suggestions as to how to solve this problem?
Add a child to the parent object, attach a collider to the child. This way you actually have 2 colliders on 1 object/area, which you can move as 1 entity.
When having 2 colliders that overlap eachother it is recommended to change the physics layer of one of them and change the transform.position.z of one of them.
The touch collider should be above the other colliders in 3dSpace.
Thought this worked but still the same problem! I'll explain in full:
I have an object called 'Ball' and this object has the polygon collider and the script with the OnTriggerEnter method.
Ball has a child object carrying the box collider with a script called 'hitbox' which references the parent object (transform.parent.gameObject) so it can do stuff when it detects a hit. The script has no OnTriggerEnter method.
But for some reason the trigger still activates when two box collider is entered! The script on the parent object doesn't reference the child or its collider at all but its OnTriggerEnter method is still picking up the collider on the child object.
Answer by Tkrain42 · Aug 11, 2015 at 01:32 PM
Put the hitbox child object on a different layer than the ball object. Make sure in the Edit|Preferences|Physics2D that the collision matrix has the hitbox child object's layer NOT colliding with the layer of the objects you want the polygon collider to trigger.
Answer by Youri1er · Aug 10, 2015 at 09:27 AM
Sorry but for me it's impossible. but there is a simple way to get around your problem.
Put your polygon collider on your sprite and put your the collider on a child of your sprite. So you need a second script on your child. But it's easy if you use a reference of that script in your first script.
Thought this worked but still the same problem! I'll explain in full:
I have an object called 'Ball' and this object has the polygon collider and the script with the OnTriggerEnter method.
Ball has a child object carrying the box collider with a script called 'hitbox' which references the parent object (transform.parent.gameObject) so it can do stuff when it detects a hit. The script has no OnTriggerEnter method.
But for some reason the trigger still activates when two box collider is entered! The script on the parent object doesn't reference the child or its collider at all but its OnTriggerEnter method is still picking up the collider on the child object.
In the script of the parent make a test to ignore children.
void OnTriggerEnter(Collider other)
{
if(other.tag != "Child_HitBox")
{
Something happen here !
}
}
Your answer
Follow this Question
Related Questions
Why is my Collider Broken? 0 Answers
[Unity 4.6.1] Weird bug between 2 Colliders 1 Answer
number of colliders inside a trigger. 2 Answers
Transform.localPosition Script Problem. 3 Answers
Bad piggies-like inventory? 0 Answers