• 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 hexdump · Nov 26, 2014 at 01:18 PM · collisionphysicscollidertriggercompound collider

Compound collider object behaving as a whole?

Hi,

Is there any possiblity for a Compound collider object to just fire an event to the parent rigidbody object?

I have a compound object made of 2 1x1 cubes (one beside the other). This 2 cubes are set as triggers and have a parent that have a rigidbody. On the other hand I have a 1x1 cube with a rigidbody attached. When I make the 1x1 cube touch the compound object (And it is touching the 2 blocks that build it up) I get two calls to OnTriggerEnter(). Is there any way to make the compound object behave as a whole without using a mesh collider?

P.D. The compound objects may have differnt shapes made of blocks, so, I really need to add more than one colliders to create the final physical shape. I won't be able to just merge the colliders and resize one to cover them all.

Cheers.

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

1 Reply

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

Answer by Bunny83 · Nov 26, 2014 at 02:24 PM

No, not that i know. They are still seperate colliders and they will detect collisions with their surface. If both collide / intersect with another collider they will both trigger an event. For what kind of functionality do you need those triggers? You could use a Dictionary to count how many times a collider is currently touching your compound trigger:

 using System.Collections.Generic;
 
 // ...
 Dictionary<Collider, int> m_Colliders = new Dictionary<Collider, int>();
 
 void OnTriggerEnter(Collider aOther)
 {
     if (m_Colliders.ContainsKey(aOther))
     {
         m_Colliders[aOther]++;
         SendMessage("OnCompoundTriggerEnter", aOther, SendMessageOptions.DontRequireReceiver);
     }
     else
         m_Colliders.Add(aOther, 1);
 }

 void OnTriggerExit(Collider aOther)
 {
     if (m_Colliders.ContainsKey(aOther))
     {
         m_Colliders[aOther]--;
         if (m_Colliders[aOther] <= 0)
         {
             m_Colliders.Remove(aOther);
             SendMessage("OnCompoundTriggerExit", aOther, SendMessageOptions.DontRequireReceiver);
         }
     }
     else
         Debug.LogError("This should never happen");
 }


This will invoke the method "OnCompoundTriggerEnter" when a collider enters one of the triggers for the first time and any additional will be ignored. When the collider exits the last trigger OnCompoundTriggerExit will be called. So those messages would behave the way you wanted. However it wouldn't work between two compound triggers.

For future questions i helps to get more information about why you need this funcionality. In many cases there are easy workarounds but without knowing what you actually want to achieve we can't suggest a solution.

Comment
Add comment · Show 4 · 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 hexdump · Nov 26, 2014 at 02:44 PM 0
Share

Hi,

Thanks for your answers.

I was thinking about implementing something like the on you suggested. But found same problem when doing a compound object vs compound object.

I will explain what I am trying to do. I have a set of blocks (always blocks) with different sizes. This blocks when positioned one beside the other (touching) will be grouped under a parent (this is the compound object).

The real main problem comes when you have two groups (two compound objects) and you move one and position it in a way that they are touching (2 blocks or more for the two groups are touching). As you may guess I will have to mergue both groups and just build one with all the blocks from group1 + group2. I want to detect this using compound colliders.

I have a version of this system where I use manual Bounding Boxes collisions but it is getting really messy for groups and eating a lot of cpu work, this is why I want to try to pass the detection phase to the physics engine that will do this in a much better way because of its space partitioning, etc... After this I have to think a good algorithm to detect the number of groups to create for example when a block is removed from a group, but this is another story and I have some working pretty close to what I need.

Cheers.

avatar image Bunny83 · Nov 26, 2014 at 03:09 PM 0
Share

@hexdump: Well, in this case you have to do a bit more work ;) You just have to treat each compound object as a whole. So you would store the rigidbody in the dictionary (which is the same for the whole group). However in this case it won't work when you touch a normal collider without a rigidbody. If you need both cases, you probably need to use two dictionaries, one with Rigidbodies and one with Colliders. Inside the OnTriggerEnter / Exit callbacks you would search for a Rigidbody upwards (the recent Unity versions even have GetComponentInParent) and if a RB is found, store it in the dictionary (or increment) and if there's no RB you do the same with the second dictionary to handle the single colliders.

avatar image hexdump · Nov 26, 2014 at 03:11 PM 0
Share

Need to think a bit about it. Let me some time to see if I will aceept the answer. Anyway, I would like to really thank you for your time and effort in trying to help.

avatar image hexdump · Nov 27, 2014 at 12:38 PM 0
Share

Well, I am using another method method taht fits bettery my needs but I understand that it answers the question I made at first so I will flag it as valid. Thanks for the help.

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

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

Related Questions

overlapsphere to destroy NPCs on exit 1 Answer

Prevent shooting when gun is inside wall 1 Answer

Trigger Colliders: Is there a way to simulate a non-trigger collider? 0 Answers

How do i get OnTriggerStay2D to work? 1 Answer

How to design an Archer Board Scoring System 1 Answer


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