• 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 davebuchhofer · Mar 16, 2010 at 07:42 PM · camerazoom

How can i mimic the "Frame Selected [F]" camera move? (Zoom Extents, Zoom to Fit)

I'm essentially trying to recreate the 'Frame Selected' built in editor function at runtime. unfortunately it seems a bit too generic of search terms to find real info on the forums anywhere!?

So, From a little searching online as best as i can find, The formula for the distance from an object at a certain field of view is something like:

set camera to look at the desired object

Distance from object = BoundingRadius/(sin(fov/2))

will post some results as I make them!

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
5
Best Answer

Answer by Jaap Kreijkamp · Mar 16, 2010 at 11:23 PM

A simple implementation in C# (made that you can click on objects to focus so you can test it, although I'm lazy so clicking only works for objects with colliders).

using UnityEngine; using System.Collections;

public class JFocusCamera : MonoBehaviour {

 Bounds CalculateBounds(GameObject go) {
     Bounds b = new Bounds(go.transform.position, Vector3.zero);
     Object[] rList = go.GetComponentsInChildren(typeof(Renderer));
     foreach (Renderer r in rList) {
         b.Encapsulate(r.bounds);
     }
     return b;
 }

 void FocusCameraOnGameObject(Camera c, GameObject go) {
     Bounds b = CalculateBounds(go);
     Vector3 max = b.size;
     float radius = Mathf.Max(max.x, Mathf.Max(max.y, max.z));
     float dist = radius /  (Mathf.Sin(c.fieldOfView * Mathf.Deg2Rad / 2f));
     Debug.Log("Radius = " + radius + " dist = " + dist);
     Vector3 pos = Random.onUnitSphere * dist + b.center;
     c.transform.position = pos;
     c.transform.LookAt(b.center);
 }

 // Update is called once per frame
 void Update () {
     if (Input.GetMouseButtonDown(0)) {
         Camera c = Camera.main;
         Vector3 mp = Input.mousePosition;
         Ray ray = c.ScreenPointToRay(mp);
         RaycastHit hit;
         if (Physics.Raycast(ray, out hit, Mathf.Infinity)) {
             FocusCameraOnGameObject(Camera.main, hit.transform.gameObject);
         }
     }
 }

}

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 davebuchhofer · Mar 18, 2010 at 11:23 AM 0
Share

Jaap! Thanks a ton, i think the Deg2Rad was screwing me up when i was trying!

avatar image GameDevBryant · Jan 23, 2019 at 09:02 PM 0
Share

Thanks! This helped me as well, it's exactly what I was looking for.

avatar image
3

Answer by Amir Ebrahimi · May 24, 2012 at 10:39 PM

The previous answer provided isn't an entirely correct answer although produces fairly good results. There are many parts involved, such as the difference in the vertical and horizontal FOVs as well as properly circumscribing the bounding volume to get the radius. This example also handles orthographic cameras:

 Bounds CalculateBounds(GameObject go) {
     Bounds b = new Bounds(go.transform.position, Vector3.zero);
     Object[] rList = go.GetComponentsInChildren(typeof(Renderer));
     foreach (Renderer r in rList) {
         b.Encapsulate(r.bounds);
     }
     return b;
 }

 public void FocusCameraOnGameObject(Camera c, GameObject go) {
     Bounds b = CalculateBounds(go);
     Vector3 max = b.size;
     // Get the radius of a sphere circumscribing the bounds
     float radius = max.magnitude / 2f;
     // Get the horizontal FOV, since it may be the limiting of the two FOVs to properly encapsulate the objects
     float horizontalFOV = 2f * Mathf.Atan(Mathf.Tan(c.fieldOfView * Mathf.Deg2Rad / 2f) * c.aspect) * Mathf.Rad2Deg;
     // Use the smaller FOV as it limits what would get cut off by the frustum        
     float fov = Mathf.Min(c.fieldOfView, horizontalFOV);
     float dist = radius /  (Mathf.Sin(fov * Mathf.Deg2Rad / 2f));
     Debug.Log("Radius = " + radius + " dist = " + dist);

     c.transform.SetLocalPositionZ(dist);
     if (c.orthographic)
         c.orthographicSize = radius;
     
     // Frame the object hierarchy
     c.transform.LookAt(b.center);
 }
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

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

2 People are following this question.

avatar image avatar image

Related Questions

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

Camera Zoom Input change. 1 Answer

How to move camera on local z axis 2 Answers

Zoom camera to correct FoV based on Rect size 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