• 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 jpierre88 · Jan 16, 2014 at 05:17 AM · camerazoom

Center, move and zoom camera to selected gameObjects

I have a 9x9 grid of gameObjects from the X and Y axis, 81 gameObjects. I want to have all of the gameObjects visible to the camera view. The camera I have is stationary and does not move for the entire game. I found myself constantly adjusting the camera's view and FOV whenever I add or remove gameObjects to the grid.

 private var rows = 9;
 private var collumns = 9;
 static var blocks : GameObject[,];
 
 
 function Start() {
     blocks = new GameObject[collumns, rows];
     var i = 0;
     for (var y = 0; y < rows; y++) {
         for (var x = 0; x < collumns; x++) {
             blocks[x,y] = GameObject.CreatePrimitive(PrimitiveType.Cube);
             blocks[x,y].transform.localScale = Vector3(0.9, 0.9, 0.9);
             blocks[x,y].transform.position = Vector3(x,y,0);
             blocks[x,y].name = i.ToString();
             blocks[x,y].tag = 'block';
             i++;
         }
     }
 }

I use the X and Y position of the gameObjects in the 2D array to make gameObjects easier and efficient to find.

Here's what I want to accomplish on runtime with the camera:

  • Calculate the center/average position point of all gameObjects to change camera's position

  • Calculate the camera's Zoom/"Field of View" for all the gameObjects to fit in the shot, without the camera zoom being too far away.

Comment
Add comment · Show 2
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 andyastro · Sep 19, 2015 at 08:02 PM 0
Share

Although this is a more than year old thread, I would like to ask if you managed to solve the second question. I am precisely stuck at this very same problem right now and could not find a proper solution as of now.

avatar image jpierre88 · Sep 20, 2015 at 12:16 AM 0
Share

@andyastro I came up with this function to calculate the bounds. I attached this script to the camera:

 public static Bounds blockBounds;
 public static int collumns = 7;
 public static int rows = 6;
 
 void OnEnable() { // Adjust camera
     blockBounds.Set$$anonymous$$in$$anonymous$$ax(new Vector3(-1,-1,0), new Vector3(collumns, rows, 0));
     while(!blocksInScreen()) GetComponent<Camera>().fieldOfView += 0.5f;
 }
 
 private bool blocksInScreen() {
     Bounds bounds = new Bounds(new Vector3(4,4,0), new Vector3(9.5f,10,0));
     float distance = (bounds.center - transform.position).z;
     float leftBorder = GetComponent<Camera>().ViewportToWorldPoint(new Vector3(0, 0, distance)).x;
     float rightBorder = GetComponent<Camera>().ViewportToWorldPoint(new Vector3(1, 0, distance)).x;
     float bottomBorder = GetComponent<Camera>().ViewportToWorldPoint(new Vector3(0, 0, distance)).y;
     float topBorder = GetComponent<Camera>().ViewportToWorldPoint(new Vector3(0, 1, distance)).y;
     return bounds.$$anonymous$$.x > leftBorder && bounds.$$anonymous$$.y > bottomBorder && bounds.max.x < rightBorder && bounds.max.y < topBorder;
 }

When I enable the camera, I set the $$anonymous$$ and max value of the new bounds for my grid and start a loop to see if the filed of view is in bounds. If it's not add 0.5f to the camera view until it fits.

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by morbidcamel · Jan 16, 2014 at 06:24 AM

Off the top of my head. To get the center you need to create a Bounds with the min and max vector of your array of objects. Once you've done that you can triangulate using the max distance on the ground (x/z) and the offset of the camera(y)

 Vector3 offset = new Vector3(0,10f,0);
 camera.transform.position = bounds.center + offset;
 var maxShotDistance = Math.Max(bounds.extends.z, bounds.extends.x):

// adjust FOV

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 jpierre88 · Jan 16, 2014 at 06:53 AM 0
Share

Sorry, I'm fairly new. Can you explain that in details about bounds in javascript?

avatar image morbidcamel · Jan 16, 2014 at 03:45 PM 0
Share

Hi, http://docs.unity3d.com/Documentation/ScriptReference/Bounds.html

This is just one way, you basically need to calculate a rectangle and find the longest edge that will fit into your camera FOV and adjust accordingly.

avatar image andyastro · Sep 19, 2015 at 08:05 PM 0
Share

Sorry for bringing a more-than-year-long thread to life, but in your answer to the OP and in the comments you've mentioned that to handle the second question of the OP, i.e. how to calculate the zoom, it would be just a matter of getting the longest edge of the bounds of an object and then "adjust accordingly". Would you care to elaborate what does that mean? I am facing the same difficulties as that of the OP. $$anonymous$$any thanks.

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

20 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

Related Questions

Pinch zoom 0 Answers

WebCamTexture zoomed in on iPad Pro 1 Answer

How to move the camera closer to the player while there is an object between them? 1 Answer

Zoom camera to correct FoV based on Rect size 1 Answer

Implementing Camera bounds proportionally to its ortographic size 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