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 /
avatar image
-1
Question by mmmudgett · Oct 04, 2013 at 11:19 AM · collisioncolliderparentchild objectcompound

How can I get Compound Colliders to work properly?

I currently have a Sphere object in the library, that I instantiate at the start of the game: alt text alt text

As you can see from the inspector of the Drone parent object, I have a rigidbody on this object. It does not have a collider. The DroneTrigger object only has a collider on it. I've read that with this setup, the child object's collider will take over for the parent if one of the parent scripts calls OnTriggerEnter. I have yet to make this work as intended.

I am trying to hit the DroneTrigger with a bullet object that I'm firing. When I was just using a collider on the Drone object, it worked fine.

I want this to eventually allow me to use a second child collider for a different collision.

EDIT: I'd like to add that when the player object enters the DroneTrigger trigger, it passes the Player collider fine. It just does not seem to like the one from the Bullet. Before you tell me, I've tried using FixedUpdate on the bullet.

drone inspector.jpg (41.2 kB)
unitydrone.jpg (10.3 kB)
Comment
Add comment · Show 3
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 mmmudgett · Oct 04, 2013 at 11:20 AM 0
Share

this is the DroneTrigger inspector:

alt text

dronetrigger.jpg (35.4 kB)
avatar image Dakwamine · Oct 04, 2013 at 12:20 PM 0
Share

What is the purpose of the Drone Trigger script? And the Drone script? Can you tell us how you are trying to use OnTriggerEnter?

avatar image mmmudgett · Oct 04, 2013 at 04:19 PM 0
Share

Right now the DroneTrigger script has no code. The Drone script has this:

 public class Drone : $$anonymous$$onoBehaviour
 {
     private Enemy$$anonymous$$anager manager;
     public int health;
     public int damage;
     public float speed;
     
     void Awake()
     {
         manager = GameObject.Find("SpawnPoint").GetComponent<Enemy$$anonymous$$anager>();
     }
     
     // Use this for initialization
     void Start ()
     {
         health = 20;
         speed = 5f;
         damage = 1;
     }
     
     // Update is called once per frame
     void Update ()
     {
         if(manager.target != null)
             transform.position = Vector3.$$anonymous$$oveTowards(transform.position, manager.target.collider.bounds.center, speed * Time.deltaTime);
     }
     
     public void OnTriggerEnter(Collider col)
     {
         Collision$$anonymous$$anager.BulletTriggerEnter(col, gameObject);
     }
 
 }



This function is in Collision$$anonymous$$anager:

 public static void BulletTriggerEnter(Collider otherCollider, GameObject drone)
     {
         if(otherCollider != null && drone != null)
         {
             if(otherCollider.tag == "Bullet")
             {
                 otherCollider.GetComponent<Bullet>().Deactivate();
                 
                 if(drone.GetComponent<Drone>().health <= 0)
                     Destroy(drone);
                 
                 else
                     drone.GetComponent<Drone>().health -= Weapon$$anonymous$$anager.Damage;
             }
         }
     }

2 Replies

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

Answer by Owen-Reynolds · Oct 04, 2013 at 03:01 PM

Triggers don't "understand" compound colliders the way other colliders do (NOTE: Unity v4.01f2.) A fix is to add a "find my parent" to them.

As you note, OnCollisionEnter is fine with a rigidbody parent with no collider, but with any number of child non-RB colliders. Hits on any kid count as hits on the parent, and you can check which exact kid it was. In code, collision.transform is the main RB object, and collision.collider is the particular kid.

But, with triggers, they only tell you the particular collider that entered/left/stayed. Even worse, If kid1 enters, and then kid2, they count both -- triggers are too "dumb" to know parentRB + kid1 + kid2 form one compound collider object.

A workaround is to find the parent yourself:

 void OnTriggerEnter(Collider cc) {
   Transform T = cc.transform;
   while(T.parent!=null) T=T.parent;

Or depending on how objects are set up, go up parents until you get the correct tag, find a rigid body ... .

Comment
Add comment · Show 2 · 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 mmmudgett · Oct 04, 2013 at 10:48 PM 0
Share

Your method works fine, it's just that I cannot get the trigger to call the OnTriggerEnter function when the Bullet enters the trigger. The Player passes its collider just fine when I run into the Drone object.

Again, the Bullet was hitting the trigger when was using just the parent object with a collider and RB.

avatar image mmmudgett · Oct 05, 2013 at 02:24 AM 0
Share

I was able to get the bullet to enter the trigger properly. I didn't realize I had collision between the Bullet layer and the Default layer off.

I'm going to add the other colliders/triggers I wanted to add, and see if I have any problems.

avatar image
0

Answer by SilentSin · Oct 04, 2013 at 01:36 PM

The OnTriggerEnter will go to either the object with the Rigidbody or the object with the collider. I'm not sure which, but evidently its the one you aren't using.

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

18 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

Related Questions

Compound collider does not trigger the parent 1 Answer

Odd character controller behavior when a shield object parented to child 0 Answers

collision.gameObject returning parent. 1 Answer

How can I check the colliders of objects instantiated into the Scene 1 Answer

Having trouble understanding parenting with componants 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