- Home /
Multiple Colliders Trigger problem
hi community,
i'm completely stuck with colliders as triggers.
i have made a script that adds a box collider to an empty gameobject and adds this as a child to a gameobject. this box collider scans nearby enemies.
so at the moment i have 2 such scripts on 1 gameobject. that means 2 colliders in 2 empty gameobject in 1 gameobject (unit). each unit has a rigidbody attached.
- unit(main gameobject)
- empty gameobject
box collider (5/5/5)
- empty gameobject
box collider (3/3/3)
- empty gameobject
the problem is when the greater box collider is triggered it automatically triggers the smaller box collider. another problem is that the OnEnter and OnExit - Trigger functions always toggle (so the same gameobject (nearby enemy) enteres and exits all the time.
i hope it is clear enough. thanks in advance.
It is not clear. You've posted no code or made mention of which game objects have which scripts.
i have one parent gameobject (unit). this has 2 empty gameobjects attached. and each one of these empty gameobjects has 1 box collider attached.
i hope it is more clearly now.
I try to explain it with images:
in the above picture you see an gameobject "enemy1" this enemy has 2 child gameobjects "bigsensor" and "smallsensor".
each of them gets a box collider attached at runtime this looks like this.
you can see that "bigsensor" has an boxcollider attached"
in my "sensor" script (that attaches the colliders to the gameobjects" i have a function:
private void OnTriggerEnter (Collider enterCollider) {
if (!enterCollider.transform.root.name.Equals ("Plane")) {
Debug.Log (this.transform.root + "/" + base.sensorName + " collided with " + enterCollider.transform.root);
}
Now the problem is that when the outter sensor collides with the first person controller the inner sensor gets also triggered
this is the function that adds a sensor to a gameobject:
public void AttachSensorBox ()
{
GameObject sensor = new GameObject(sensorName);
sensor.transform.parent = this.transform;
this.sensorBox = sensor.AddComponent<BoxCollider> ();
this.sensorBox.size = this.sensorBoxVector;
this.sensorBox.isTrigger = true;
this.sensorBox.center = sensor.transform.parent.transform.position;
this.sensorBoxCenterOrigin = this.sensorBox.center;
this.sensorBox.center += this.sensorBoxOffset;
int layerNumber = Layer$$anonymous$$ask.NameToLayer(this.layerName);
this.sensorBox.gameObject.layer = layerNumber;
}
this script is attached to the "enemy1" gameobject
i hope you get the point thanks in advance
On which gameObject(s) do you have the OnTriggerEnter function added? I might be wrong, but it sounds like you have the OnTriggerEnter added to the parent ins$$anonymous$$d of two scripts each added to those sensors.
At last I don't see any code in the "AttachSensorBox" function that would add another script to the sensor that contains the OnTriggerEnter function..
Answer by gamedev99 · Jan 15, 2014 at 08:50 PM
i have just tried to the following:
add the "sensors" to each child object...now it works only if i add a rigidbody to each child object....but i think this is not the way it should work cause the rigidbodies fall through the actual sensors....
okay now it works fine! you were totally right! the solution to my problem was: attach the script which attaches the sensor (collider) to each of the child gameobjects then check in a parent OnTriggerEnter() function
thank you so much...you brought me on the right way ;)
I don't think that you actually need a RigidBody for those sensors, just add a collider and check the "is Trigger" checkBox somewhere in the settings.. :)
Answer by lattman · Apr 24, 2015 at 04:00 AM
I have run into this same problem and am posting with hopes that this will help somebody understand the problem and come up with a better work around.
from the Unity Docs: Collider.OnTriggerEnter(Collider)
This message is sent to the trigger collider and the rigidbody (if any) that the trigger collider belongs to, and to the rigidbody (or the collider if there is no rigidbody) that touches the trigger. Note that trigger events are only sent if one of the colliders also has a rigidbody attached.
the problem can be caused if (similar to the screenshots above) you have a rigidbody on a parent object, a trigger collider on the parent, a script on the parent that uses the trigger events, and a child object that has a trigger collider.
According to the docs the rigidbody is notified of the trigger collision, even if it is in the parent and has nothing to do with the specific trigger on the child. In my situation the child and the parent's OnTriggerEnter() were both called when the player touched the sensor. The sensor area was for player detection. The Trigger on the parent was for detecting touching the player and dealing damage.
The work around which is mentioned above is to put a rigidbody on the sensor area child object. This seems to force the sensor area to use up the collision event and avoids relaying it to the parent's rigidbody. I don't like this solution at all because it relies on an non needed component. Hopefully somebody knows of a better way.
Your answer
Follow this Question
Related Questions
Problems with the triggers(not recognized) 1 Answer
Internal collisions 1 Answer
C# Make two objects collide to activate a third object 3 Answers
Return gameobject from a TriggerEvent. 1 Answer
Trigger and Collider? 3 Answers