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
0
Question by Shchurov · Sep 25, 2016 at 06:18 PM · cameraboundsaspect ratio

Calculate camera position so the game field is completely visible

I'm making a game where everything happens on a single game field. And in the beginning I would like to calculate the most optimal camera position, i.e. the game field takes as much space on the screen as possible.

So here's an example of almost optimal camera position (almost because I set it manually): alt text

And here's another example where the screen has different aspect ratio. Those paddings on the sides are not optimal: alt text

How to calculate the camera position if I know its rotation and the size of game field? Tried to solve it on paper for a couple of days but can't even draw it properly.

1.jpg (64.2 kB)
2.jpg (61.4 kB)
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

1 Reply

· Add your reply
  • Sort: 
avatar image
1

Answer by kidmosey · Sep 25, 2016 at 10:05 PM

I need this for my game, so I went ahead and whipped up something for you. It should probably be changed to just return the target position instead of actually moving the camera, though.

 /// <summary>
 /// Position the camera so an object's bounding box fills the screen
 /// </summary>
 /// <param name="zoomToObject">The object that should be focused to</param>
 /// <param name="cam">The camera to move</param>
 public void ZoomToObject(MeshRenderer zoomToObject, Camera cam)
 {
     // move the camera to the object
     cam.transform.position = zoomToObject.transform.position;
 
     // project a ray from each of the bounding coords to each of the frustum planes
     float maxDistance = 0;
     Plane[] frustumPlanes = GeometryUtility.CalculateFrustumPlanes(cam);
     Plane[] frustumPlanesToUse =
     {
         // [0] = Left, [1] = Right, [2] = Down,
         // [3] = Up,   [4] = Near,  [5] = Far
         frustumPlanes[0], frustumPlanes[1],
         frustumPlanes[2], frustumPlanes[3]
     };
 
     foreach (Vector3 v in zoomToObject.bounds.GetVertices(true))
     {
         Ray ray = new Ray(v, cam.transform.forward);
         foreach (Plane p in frustumPlanesToUse)
         {
             float enter;
             if (p.Raycast(ray, out enter))
             {
                 if (enter > 0 && enter > maxDistance)
                 {
                     maxDistance = enter;
                 }
             }
         }
     }
 
     // pull the camera back
     cam.transform.position -= cam.transform.forward * maxDistance;
 }
 
 /// <summary>
 /// Return all of the vertices associated with a bounding box
 /// </summary>
 /// <param name="bounds"></param>
 /// <param name="includeCenter">whether to also include the center point</param>
 /// <returns></returns>
 public static IEnumerable<Vector3> GetVertices(this Bounds bounds, bool includeCenter)
 {
     Vector3 v1 = bounds.min, v2 = bounds.max;
     yield return new Vector3(v1.x, v1.y, v1.z);
     yield return new Vector3(v1.x, v2.y, v1.z);
     yield return new Vector3(v2.x, v2.y, v1.z);
     yield return new Vector3(v2.x, v1.y, v1.z);
 
     yield return new Vector3(v1.x, v1.y, v2.z);
     yield return new Vector3(v1.x, v2.y, v2.z);
     yield return new Vector3(v2.x, v2.y, v2.z);
     yield return new Vector3(v2.x, v1.y, v2.z);
 
     if (includeCenter)
         yield return bounds.center;
 }
 
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 Shchurov · Sep 26, 2016 at 08:56 PM 0
Share

It looks promising, will try it and post the final version here. $$anonymous$$any thanks!

avatar image Shchurov · Sep 27, 2016 at 08:18 PM 0
Share

Here's the result. I think there should be some correction depending on the camera angle. alt text

3.jpg (43.8 kB)

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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

Camera Bounds 2 Answers

Keep Cursor In Bounds of Camera 2 Answers

Capture Bounds with an orthographic camera -- runtime world map 1 Answer

Keep player from falling outside the camera? [2d] 1 Answer

CustomCameraController: Rotating my camera breaks my out of bounds code 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