- Home /
Only take into account colliders' bounds on single gameobject in hierarchy
Hello again! I've terrible luck in getting my questions answered, since i ask for such damned difficult things, but let's give this a shot again.
I need to find a way on how to only take into account a singular gameobject's colliders' bounds, in a scenario where there are children that have colliders too.
I have a script, which does some physics stuff (would take focus away from question to explain further) to an object with a rigidbody attached, based on Collider.bounds. The issue i have, is that if i happen to have a child with colliders attached to it, it takes into account the child's colliders too. The usual fix would be to plant a kinematic rigidbody onto this child object, but since i'm already fooling around with physics, i can't do that, since it affects how the object behaves.
It also takes into account any triggers, for that matter, which is strange, but not actually an issue at this point.
I have a workaround, however, but it is not optimal at all, as i'd need to do this to every last collider i want manually, and not all shapes work fine with it:
public BoxCollider CollFrame; //Set in editor
void Start() {
Bounds b;
if (CollFrame == null)
{
b = collider.bounds;
}
else
{
b = CollFrame.bounds; //Must set this, otherwise it gets grumpy.
b.center = CollFrame.center;
b.extents = CollFrame.size / 2;
b.max = CollFrame.center + b.extents;
b.min = CollFrame.center - b.extents;
b.SetMinMax(b.min, b.max); //This seems to be necessary
b.size = CollFrame.size;
}
foo(b);
}
Does anyone have a nice way of getting around the usual method Collider.Bounds work, so that it can ignore its children? The problem is that my implementation specifically uses the Collider.Bounds, it doesn't use OnTrigger or OnCollision events, so usual fixes don't apply.
You could move and rotate those children with their parent without childing them to it. Would keep physics clean and won't add colliders up.
Do your children object have box colliders as well?. Have you tried to change the collider to other type of collider?. Have you tried to disable the child colliders just before getting the bounds and enabling them back once you have the data?
@hexagonius That certainly is one possible way... is there a very efficient way of doing this, however? I'm mostly worried of the performance hit of forcing an object to follow another strictly.
@spiceboy9994 Whether they do or don't, it will still take into account their collider's sizes in Collider.Bounds. It really depends on the object.
Basically the scenario is that i want to have between one to a few trigger colliders, let's say a convex mesh collider for example, which i want to use for physics calculations utiling its colliders' shape and bounds... and then i have the actual colliders for world interaction, which' i don't want to interact with the physics calculations... but they still do. They even shift the center of mass.
I don't think so. Just save relative position and rotation difference to the parent and adjust both if the parent moved. But you'll have to test that. I think this will only be expensive on mobile, if even.
@hexagonius The main problem is more of a network performance hit, in testing that. While the actual processing performance hit isn't noticable until there's around 100 children, which honestly isn't too many, if you're using depthmasks and the like.
Answer by maccabbe · Feb 25, 2015 at 04:37 PM
I would say the easiest way to make colliders not interact with other colliders is to use Physics settings, accessible from Edit->Project Settings->Physics (or Physics2D if you are using 2d).
From this menu, you can edit the layer collision matrix. So if you set all children in the hierarchy to be on the "Player" layer and disable Player x Player in the layer collision matrix, none of the children in the hierarchy will collide with other children with the hierarchy.
Edit: Sorry, I totally did not understand the original question. Have you tried unparenting all the children while calculating the bounds?
void Start() {
List<Transform> children=new List<Transform>();
foreach(Transform child in transform) {
children.Add(child);
}
foreach(Transform child in children) {
child.transform.parent=null;
}
// code here
foreach(Transform child in children) {
child.transform.parent=transform;
}
}
Thanks for the answer!
This isn't actually the problem, however. Children aren't as much colliding with the parent, the main problem is that the Collider.bounds calculates -all- colliders contained in the hierachy, and so far the only way i've found to get around this is to manually extract the collision's sizes per collider. This can be taxing to do if there's several of them, and i can't find a smart way of doing it programmatically, outside of literally doing "GetComponents" for all possible collidertypes, and then calculating their combined mass for the parent only.
This is... quite a performance hit, which is the main problem with that.
Unparenting the children actually works beautifully with a completely neglible performance hit! Now why didn't i think of a super simple solution like that?
Thank you :)
I kind of hope that in Unity 5, it's at least optional to choose whether or not to include child components' colliders in Collider.Bounds.
Your answer
Follow this Question
Related Questions
HingeJoint with no rotation? 0 Answers
ignorecollision return error 0 Answers
2D Sprites don't match the position of the 2DColliders 0 Answers
Very simple collision death 1 Answer
Random results with physics.OverlapSphere and AOE attacks 0 Answers