Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
This post has been wikified, any user with enough reputation can edit it.
avatar image
0
Question by gamedev99 · Jan 07, 2014 at 09:28 PM · gameobjectcollidertriggerbox

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.

  1. unit(main gameobject)
    1. empty gameobject
      1. box collider (5/5/5)

    2. empty gameobject
      1. box collider (3/3/3)

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.

Comment
Add comment · Show 5
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image iwaldrop · Jan 08, 2014 at 02:43 AM 0
Share

It is not clear. You've posted no code or made mention of which game objects have which scripts.

avatar image gamedev99 · Jan 08, 2014 at 08:43 PM 0
Share

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.

avatar image ShadoX · Jan 09, 2014 at 08:09 AM 0
Share

Probably would help if you'd post a code snippet..

avatar image gamedev99 · Jan 14, 2014 at 06:20 PM 0
Share

I try to explain it with images:

alt text

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.

alt text

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

avatar image ShadoX · Jan 15, 2014 at 07:38 AM 0
Share

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..

2 Replies

· Add your reply
  • Sort: 
avatar image
0

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 ;)

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image ShadoX · Jan 16, 2014 at 11:37 AM 0
Share

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.. :)

avatar image
0

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. alt text

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.


screenshot.png (132.1 kB)
Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

22 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges