- Home /
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.
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.
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.
@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.
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.
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.