Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 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
7
Question by Extrakun · May 24, 2010 at 07:13 AM · 3d

Finding the bounds of a grouped model

I am importing a 3d model which consists of many smaller sub-parts; I would like to have a script to fire whenever the user clicks on any of the sub-part of the model. I could take a a cube gameobject and enclose it and disable the rendering, but I will have to manually size the cube.

Is there anyway to calculate the bounding area of such a grouped model? And how could I scale the cube to match that bounding area?

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

2 Replies

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

Answer by Eric5h5 · May 24, 2010 at 08:43 AM

You can iterate through the objects and use Encapsulate to add to the total bounds from the renderers:

var combinedBounds = renderer.bounds;
var renderers = GetComponentsInChildren(Renderer);
for (var render : Renderer in renderers) {
    if (render != renderer) combinedBounds.Encapsulate(render.bounds);
}

That returns the bounds in world space; you can use mesh.bounds to get bounds in local space.

Comment
Add comment · Show 6 · 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 Extrakun · May 25, 2010 at 07:32 AM 0
Share

How do I assign this bound correctly to the parent object so that its collider's size can encapsulate the child objects? Should I use BoxCollider.size?

avatar image Eric5h5 · May 25, 2010 at 08:26 AM 0
Share

@Extrakun: use collider.bounds, and use type Collider with GetComponentsInChildren.

avatar image HeywoodFloyd · Sep 08, 2010 at 12:03 AM 0
Share

This code won't work unless all the all maxima have all positive values and all $$anonymous$$ima have all negative values. You need to define the cumulative bounds by the bounds of the first render.

avatar image Eric5h5 · Sep 08, 2010 at 02:27 AM 1
Share

@HeywoodFloyd: good point; I fixed the code

avatar image code-blep · Dec 07, 2012 at 03:38 PM 0
Share

Once again Eric5h5 to the rescue. I spent ages looking into this...

Show more comments
avatar image
11

Answer by tgouala-wellfiredLtd · Feb 27, 2016 at 03:57 PM

Here a snippet to get the local bounds which works for any model, no matter it is rotated or not :

     private void CalculateLocalBounds()
     {
         Quaternion currentRotation = this.transform.rotation;
         this.transform.rotation = Quaternion.Euler(0f,0f,0f);
 
         Bounds bounds = new Bounds(this.transform.position, Vector3.zero);
 
         foreach(Renderer renderer in GetComponentsInChildren<Renderer>())
         {
             bounds.Encapsulate(renderer.bounds);
         }
 
         Vector3 localCenter = bounds.center - this.transform.position;
         bounds.center = localCenter;
         Debug.Log("The local bounds of this model is " + bounds);
 
         this.transform.rotation = currentRotation;
     }

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 Toemsel92 · May 25, 2018 at 11:34 PM 0
Share

Thanks. Your approach did work for me :)

avatar image naetib · Feb 19, 2019 at 04:54 AM 0
Share

this can apply for case that we want to add a box collider to a gameobject (3d model) which consists of many smaller sub-parts

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

6 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

The name 'Joystick' does not denote a valid type ('not found') 2 Answers

Adding force to a bullet 3 Answers

Changing 3d text through script 1 Answer

Creating a bounding box for multiple objects using bounds 2 Answers

3D Menu Help 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