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
1
Question by pretender · Dec 16, 2010 at 01:38 PM · boundsgroupcalculate

calculating the object bounds of the group of objects

i am trying to get the center of the group of objects, so that i can center the camera to that group and have all subobjects in the screen.

here is the code i am using:

var bound : Bounds= new Bounds(Vector3.zero,Vector3.zero); //all objects in the group have colliders, so i am using colliders to get bounds var Colliders = target.GetComponentsInChildren(Collider);

for(var collider : Collider in Colliders){ //if members are not tagged group or microlabel since these are just groups if(collider.tag!="group" || collider.tag!="microlabel"){ bound.Encapsulate(collider.bounds); } }

//print the center and size of the new bounding box Debug.Log("center: " + bound.center.ToString() + " size: " + bound.size.ToString());

//test the center of bounding box by creating the sphere with that position var sphere : GameObject=GameObject.CreatePrimitive(PrimitiveType.Sphere);

sphere.transform.position=bound.center; //make sphere smaller so it fits the scene and marks more precisely the position sphere.transform.localScale=Vector3(0.2,0.2,0.2);

this works fine for group of groups of objects, but it does not calculate the group that only holds objects i have offset by the y axis, what to do? maybe i am missing something? how to calculate this type of thing properly?

thanks

EDIT:

ok i guess that i should use the mesh.bounds instead of collider bounds. it is harder to get those i think. can somebody help me replace collider bounds with mesh.bounds in the code i provided. any help appreciated!

Comment
Add comment · Show 1
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 pretender · Dec 16, 2010 at 02:40 PM 0
Share

using renderer.bounds ins$$anonymous$$d of collider.bounds gives the same result any help appreciated!

1 Reply

· Add your reply
  • Sort: 
avatar image
4

Answer by Ejlersen · Dec 16, 2010 at 08:07 PM

If you want to use the mesh.bounds, then you need to do something like this:

var filters = target.GetComponentsInChildren(MeshFilter);

for(var filter : MeshFilter in filters) { //if members are not tagged group or microlabel since these are just groups if(filter.tag!="group" || filter.tag!="microlabel") { bound.Encapsulate(filter.mesh.bounds); } }


I think that would work. Its of the top of my head :)

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 pretender · Dec 16, 2010 at 10:39 PM 0
Share

it doesn not makes spheres at correct position... maybe it has something to do with local space it dont know...so far using colliders at least gives me approximate position... do not know what is going on

avatar image Ejlersen · Dec 17, 2010 at 11:14 AM 0
Share

No, you are correct. This is from the documentation for mesh.bounds:

"This is the axis-aligned bounding box of the mesh in its local space (that is, not affected by the transform). See also Renderer.bounds property that returns bounds in world space."

So try getting the $$anonymous$$eshRenderers ins$$anonymous$$d and use their bounding box.

Hope this solves your problem.

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

No one has followed this question yet.

Related Questions

when should Mesh.Optimize() be called 1 Answer

How to calculate a Collider2D's bounds 0 Answers

get the height of a sprite at a specific point 0 Answers

Grouping Enemies/Targets 1 Answer

2D GUI Group moving with players. 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