Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 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 /
avatar image
26
Question by buscommando · Mar 04, 2013 at 08:27 PM · collidersontriggerentertriggerschildren

Trigger in child object calls OnTriggerEnter in parent object

The parent object is a small cube with a box collider trigger to fit and a kinematic rigidbody. the child of that object is an empty with a sphere collider trigger that is larger than the box collider.

different scripts are in both parent and child with OnTriggerEnter functions.

expected behavior: when the larger sphere collider (a component of the child) is triggered without triggering the parent, OnTriggerEnter is called in the child object's script.

observed behavior: when the larger sphere collider is triggered, OnTriggerEnter is called in both the parent and the child scripts.

Comment
Add comment
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

3 Replies

· Add your reply
  • Sort: 
avatar image
71
Best Answer

Answer by hoy_smallfry · Mar 04, 2013 at 09:47 PM

Read the section labeled "Compound Colliders" on this page.

Long story short, collider shapes can't be concave and the only way to accomplish something concave is to treat all convex colliders in children under a parent as if they are one shape, if the parent is the only one with a rigidbody. So, your parent is treating it as if its a complex shape, hence OnTriggerEnter in your parent is getting called.

Try this: give the child a kinematic rigidbody. This will separate it from the parent collision, but still allow it to be moved by the parent's transform instead of physics.

However, be aware that if one touches or encompasses the other, now that they are seen as separate entities, a collision trigger will get registered between the two, even if it's just at the beginning of the scene. They won't bounce off each other since one is a trigger, but you may want to have each OnTriggerEnter function ignore the other Collider if it matches the tag of the other:

 // in the child's function:
 void OnTriggerEnter(Collider other)
 {
     if (other.tag == "Parent's Tag")
         return; // do nothing

     Debug.Log("child goes ouch!");
 }

 // in the parent's function:
 void OnTriggerEnter(Collider other)
 {
     if (other.tag == "Child's Tag")
         return; // do nothing

     Debug.Log("parent goes ouch!");
 }

I hope that helps!

Comment
Add comment · Show 13 · 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 buscommando · Mar 05, 2013 at 02:34 AM 1
Share

I will experiment with this. Thank you much!

The parent's trigger matches the parent's mesh exactly, if something triggers it the parent dies.

The child's trigger is a spatial buffer - if something triggers the child's collider, the parent moves/runs away from the object that triggered it.

I had not considered giving both the parent and the child I$$anonymous$$ rigidbodies, currently only the parent has a rigidbody.

$$anonymous$$y solution in the interim was to remove the collider from the parent and add a second child with its own OnTriggerEnter script and a trigger collider identical to the parent's old collider. It worked but felt kludgey, your solution seems a bit more elegant.

avatar image kalibcrone buscommando · Jul 13, 2017 at 01:06 PM 2
Share

Just a friendly note, "I$$anonymous$$" stands for "Inverse $$anonymous$$inematics" and doesn't really mean the same thing in this context of a $$anonymous$$inematic RigidBody. The term I$$anonymous$$ is generally used when talking about rigging a model for animation.

avatar image pas0003 · Jun 03, 2013 at 10:27 AM 0
Share

Thank you so much for that tip!! :D

avatar image filip_andry · Jul 01, 2014 at 06:58 AM 0
Share

Thanks, It worked for me.

avatar image einzweidrei · Feb 24, 2015 at 10:42 AM 1
Share

Yes, $$anonymous$$inematic Rigidbody did it exactly. Thanks, this made it work finally after long period of banging my head around. :-)

avatar image dgreedy · Apr 29, 2015 at 11:23 PM 0
Share

Buscommando's solution of seperating the triggers and trigger scripts into separate children of the parent object worked well for me. Thanks.

Show more comments
avatar image
7

Answer by luislodosm · Aug 27, 2016 at 02:34 PM

Problem:

  • Parent with collider + rigidbody

  • Child with collider

Colliders will combine.

A solution:

  • Parent empty

  • Child with collider + rigidbody

  • Child with collider + follow script

       public GameObject followThisObject;
     
         void Update () {
             transform.position = followThisObject.transform.position;
         }
    
    
    
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 MikaLikeABoss · Nov 27, 2019 at 01:58 AM 0
Share

Warning: The Rigidbody at child GameObject will move that game object, but not its parent. It appears to work because parent game object stays in place, at the center of the world, while second child follows first child. Visually, it appears correct, but semantically it is wrong. Correct version is:

  • Parent empty + follow script

  • Child with collider + Rigidbody

  • Child with collider

Also, when the parent is following the child, this is required:

 parent.transform.position = followedChild.transform.position;
 followedChild.transform.localPosition = Vector3.zero;

Because when you set the parent's position to previous child position, the child will also move to maintain the relative distance to its parent. You have to reset the child's position. For most situations Vector3.zero is O$$anonymous$$, but each coder knows what he/she intents to do, so adjust as desired.

avatar image
0

Answer by ossetegames · Sep 30, 2018 at 02:24 PM

I had to find another way to solve this because as a complete noob, I didn't know about this so my entire code was designed to have a parent collider + RB + child object with a Trigger, just like you, I think. The proposed solution would have a really huge impact on the rest of the code.

To give you some context: I have my main actor and its child trigger. The trigger is to allow it to reach some objects close to it. I also needed to detect when the main's collider entered a zone (a large trigger on the level) to call mainActor.DoMyStuff() and of course, both the child trigger and the collider were raising the OnTriggerEnter2D, therefore, calling the method twice.

So what I did is I added a tag on the parent and a different tag on the child trigger. Then I added the OnTriggerEnter2D on the level trigger (the zone I need the actor to enter) and there, I CompareTag to do something when it's the parent that has entered (ignoring the child basically). In your case, I guess you want to do something if the child trigger is reached by something so using this alternate solution you'd had to add a script to the trigger, maybe only to reference the main actor object. Then, in your bullet's OnTriggerEnter, CompareTag and if it's the trigger, call TriggerScript.MainReference.DoYourStuff();

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

23 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 avatar image

Related Questions

When player enters triggerzone call function 2 Answers

Transform child out of bounds. 0 Answers

Trigger on Child making calls to OnTrigger in Parent 2 Answers

On Trigger enters being called a frame late 1 Answer

Does onTriggerEnter work with child colliders? 0 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