Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 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.

Follow this Question

Answers Answers and Comments

2 People are following this question.

avatar image avatar image

Related Questions

How to do perspective camera zoom? 1 Answer

Changing camera position to shoot through scope on gun 1 Answer

Raycast misses after camera zoom 2 Answers

Lining up Fov's 0 Answers

Any ideas for doing a digital camera zoom? 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