• 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 efergan · Jun 27, 2012 at 01:06 PM · cameragameobjectsbounds

Bounds Around Multiple Targets

Hi All,

After some searching about on this forum (and others) I still find myself perplexed. I hope this isn't reiterating another asked question. The nearest I found was this: http://answers.unity3d.com/questions/38417/calculating-the-object-bounds-of-the-group-of-obje.html , but this doesn't seem to quite do it.

Basically I'm attempting to find the total bounds of a number of independent game objects, so I can point my camera at them and make sure the player isn't missing any of the action.

It's my approach rather than the code I'm questioning, so rather than drop in a load of code, here's what I'm attempting to do:

  • Gather all the GameObjects of interest in one list

  • Encapsulate the GameObjects within one Bounds

  • Use this Bounds to move and zoom the main camera

The two problems with this approach I've found:

Bounds.Encapsulate() will only work if you've centered the Bounds correctly first. But to do that you need to do some maths upfront. I'd be happy to strip out the X & Z positions and then work out the center, but this feels messy to me. Is there not a way to group everything and basically say: Give me the total Bounds.

Bounds only deals with a single Axis. This is not a problem right now (the game is similar to an RTS game, so ground based units), but might become one later where things high up could be missed.

Am I going about this in the right way? Is there a better technique to understand the space in which a group of objects exists within?

Thanks in advance everyone.

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

4 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by efergan · Jun 28, 2012 at 07:14 AM

Thank you all.

So I realise I'd tried using the first objects bounds prior, but had stupidly forgot to reset the bounds each update, therefore making it pointless. I've ended up with this code, which seems tobe working...

     bool isReset = false;
     foreach (GameObject gameObject in theAction) {
         if (isReset == false) { 
             actionView = new Bounds(gameObject.transform.position, new Vector3(1,1,1));
             isReset = true;
         }
         actionView.Encapsulate(gameObject.transform.position);
     }

Where theAction is a List of Game Objects, and Action View is the bounds.

You may be able to help with a small niggle. I was trying to simply reset actionView to null, and test for actionView = null rather than a seperate bool, isReset. But due to null not being of Type Bounds I get an error. How would you reset a Bounds variable?

Thanks again

Comment
Add comment · 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
0

Answer by aldonaletto · Jun 27, 2012 at 01:41 PM

You could calculate the bounds center of a GameObject array with the function below:

function CalcCenter(objs: GameObject[]): Vector3 {
  var min = Vector3.one * Mathf.Infinity;
  var max = Vector3.one * Mathf.NegativeInfinity;
  for (var obj: GameObject in objs){
    var box = obj.renderer.bounds; // or use obj.collider.bounds
    min = Vector3.Min(min, box.min); // expand min to encapsulate bounds.min
    max = Vector3.Max(max, box.max); // expand max to encapsulate bounds.max
  }
  return (max+min)/2;
}
Get the objects of interest in an array, then call CalcCenter to get the center point. I suppose renderer.bounds is what you need, since this will contain all the mesh - collider.bounds may be smaller or greater than the mesh.
Comment
Add comment · Show 3 · 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 DragonBorn007 · May 28, 2015 at 08:37 PM 0
Share

Whats the C# equivalent? I'm getting confused by the use of $$anonymous$$athf.Infinity/$$anonymous$$athf.Negative.Infinity. Are they arguments to Vector3.one ? Is there a missing operator?

avatar image Paulius-Liekis · May 28, 2015 at 09:15 PM 0
Share

Yeah, looks like bad formatting or something. I think it's missing multiplication between those two.

avatar image Erikoinen · Nov 14, 2016 at 09:13 PM 0
Share

Oldie but goldie. Thank you. :)

avatar image
0

Answer by Flufflesthepancake · Jun 27, 2012 at 01:28 PM

I have a way that you might implement for your problem.

 objectArray = GameObject.FindGameObjectsWithTag("objectTag");

Get all your objects into the array, however you will, with tags or what you are using.

         xtempmax = objectArray[0].transform.position.x;
     for (int i = 1; i < objectArray.Length; i++) {
         if (objectArray[i].transform.position.x > xtempmax) {
             xtempmax = objectArray[i].transform.position.x;
         }
     }
     xtempmin = objectArray[0].transform.position.x;
     for (int i = 1; i < objectArray.Length; i++) {
         if (objectArray[i].transform.position.x < xtempmin) {
             xtempmin = objectArray[i].transform.position.x;
         }
     }
     //Y AVERAGE
     ytempmax = objectArray[0].transform.position.y;
     for (int i = 1; i < objectArray.Length; i++) {
         if (objectArray[i].transform.position.y > ytempmax) {
             ytempmax = objectArray[i].transform.position.y;
         }
     }
     ytempmin = objectArray[0].transform.position.y;
     for (int i = 1; i < objectArray.Length; i++) {
         if (objectArray[i].transform.position.y < ytempmin) {
             ytempmin = objectArray[i].transform.position.y;
         }
     }
     //Z AVERAGE
     ztempmax = objectArray[0].transform.position.z;
     for (int i = 1; i < objectArray.Length; i++) {
         if (objectArray[i].transform.position.z > ztempmax) {
             ztempmax = objectArray[i].transform.position.z;
         }
     }
     ztempmin = objectArray[0].transform.position.z;
     for (int i = 1; i < objectArray.Length; i++) {
         if (objectArray[i].transform.position.z < ztempmin) {
             ztempmin = objectArray[i].transform.position.z;
         }
     }

Get your max and min bounds for all your things.

Then move the camera based off of those, for example if you wish to centre the camera:

Vector3 newpos = new Vector3((xtempmax+xtempmin)/2, (ytempmax+ytempmin)/2, (ztempmax+ztempmin)/2);

You have the average of those positions stored in newpos and you can lerp the camera to that position.


Although you're doing it "the long way" sometimes it ends up being much more flexible in the end. I had trouble with getting bounds working properly with one of my games and this is what I used instead.

Comment
Add comment · Show 1 · 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 Paulius-Liekis · Jun 27, 2012 at 01:43 PM 0
Share

Aw..c'mon, this is such ineffective way. You're looping through all objects 6 times. And you accessing transform.position each time..twice, which means it iterates to the up-most parent. Identical code using Bounds would be like 5 lines... And 12 times faster...

avatar image
4

Answer by Paulius-Liekis · Jun 27, 2012 at 01:14 PM

Do not initialize your bounds to zero - take initial bounds from first object and then expand it using other objects. This way bounds won't include (0,0,0) point unless it has to.

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 calebhc · Dec 09, 2013 at 05:21 PM 0
Share

Thank you so much for this solution! I was spending way too long trying to figure out why my calculated bounds were getting all screwed up. :)

avatar image Saidbet · Nov 28, 2017 at 10:29 AM 0
Share

ty <3 Wasted too much time trying to figure out the 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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

9 People are following this question.

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

Related Questions

This camera controller is overriding it's default position when the boundary check is enabled? 0 Answers

Shuriken won't render off camera bounds 0 Answers

put prefab in top left of the camera 0 Answers

Get screen bounds with **rotated** ortho camera?? 2 Answers

Want to frame multiple objects in camera view using render bounds 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