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
0
Question by Nymisu · Feb 25, 2015 at 03:09 PM · physicscolliderbounds

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.

Comment
Add comment · Show 5
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 hexagonius · Feb 25, 2015 at 03:31 PM 0
Share

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.

avatar image spiceboy9994 · Feb 25, 2015 at 03:52 PM 0
Share

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?

avatar image Nymisu · Feb 26, 2015 at 08:23 AM 0
Share

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

avatar image hexagonius · Feb 26, 2015 at 09:26 AM 0
Share

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.

avatar image Nymisu · Feb 26, 2015 at 11:06 AM 0
Share

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

1 Reply

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

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;
     }
 }
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 Nymisu · Feb 26, 2015 at 08:19 AM 0
Share

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.

avatar image Nymisu · Feb 26, 2015 at 10:43 AM 0
Share

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

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

21 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

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


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