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 satchel82 · Aug 19, 2016 at 04:07 PM · cameraboundsorthographic

Fitting Bounds into Orthographic 2d Camera.

Hi!

I have a top down 2d game, and I am trying to figure out how to set the camera. As with everything, I google for a long time, but none of the answers so far have given me what I need.

Given a Bounds object of any size, how would one fit that bounds perfectly into the camera?

I also need to create a Margin but I guess that is pretty simple.

Thanks!

Edit:

I realised how broad this was. So to put it another way.

 float boundsToOrthographicSize(Bounds bounds, float margin){
        //magic here
 } 

Additional references:

  • Adjust Orthographic Camera to fit object

  • Resizing orthographic camera to fit 2d sprite on screen

  • Making a Target Tracking Orthographic Camera in Unity

On that last one. I cannot seem to convertBoundsToRect to plug into the rest of it. So answering that, may answer this!

One last thing. it may be portrait or landscape, so you cannot assume that width is the first class citizen.

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
4

Answer by satchel82 · Aug 22, 2016 at 05:08 AM

How to calculate the orthographicSize given any bounds.

 public class CameraTrackBounds: MonoBehaviour
 {
     //the Bounds object we want the camera to follow
     public Bounds targetBounds;
 
     void LateUpdate()
     {
 
         float screenRatio = (float)Screen.width / (float)Screen.height;
         float targetRatio = targetBounds.size.x / targetBounds.size.y;
 
         if (screenRatio >= targetRatio)
         {
             Camera.main.orthographicSize = targetBounds.size.y / 2;
         }
         else
         {
             float differenceInSize = targetRatio / screenRatio;
             Camera.main.orthographicSize = targetBounds.size.y / 2 * differenceInSize;
         }
 
         transform.position = new Vector3(targetBounds.center.x, targetBounds.center.y, -1f);
 
     }
 
 }

The results are thus.

Landscape Portrait

I am not finished this with this answer yet as I need to get these things smoothed and figure out a "border".


1.jpg (34.6 kB)
2.jpg (27.5 kB)
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 NovacainE2 · Jan 24, 2017 at 05:42 PM 0
Share

Hello mate @satchel82 ! I don't know if you see this but since yours, is probably the only solution that worked for me, I figured I asked out for some help. I am developing a 2D game for android and while your solutions works...it still is not right for me.

Anyways, what I am trying to achieve here is make the size of camera fit the edges of my phone. I have figured out how to make an adjustable aspect ratio but the thing is that the x bounds don't close on my edges as I would like to. There is still a lo of gap while the vertical extent seems to be working fine.

As a prt screen shows, all the black around my green space is actually the camera size How can I make the size of the camera fit just the green space?

I am working with randomly generated prefabs(2D sprites) all of which are under a parent. The bounds I have linked to your script are of a Plane's attached on top of the parent and matching the dimensions of the parent.

Still not luck. :(alt text

screenshot-24.png (4.3 kB)
avatar image
0

Answer by Nirav-Madhani · May 05, 2018 at 02:39 PM

This is what i found while googling.. It think you mean this Source:https://gist.github.com/RyanNielson/9879056

 using UnityEngine;
 
 public class TrackTargets : MonoBehaviour {
 
     [SerializeField] 
     Transform[] targets;
 
     [SerializeField] 
     float boundingBoxPadding = 2f;
 
     [SerializeField]
     float minimumOrthographicSize = 8f;
 
     [SerializeField]
     float zoomSpeed = 20f;
 
     Camera camera;
 
     void Awake () 
     {
         camera = GetComponent<Camera>();
         camera.orthographic = true;
     }
 
     void LateUpdate()
     {
         Rect boundingBox = CalculateTargetsBoundingBox();
         transform.position = CalculateCameraPosition(boundingBox);
         camera.orthographicSize = CalculateOrthographicSize(boundingBox);
     }
 
     /// <summary>
     /// Calculates a bounding box that contains all the targets.
     /// </summary>
     /// <returns>A Rect containing all the targets.</returns>
     Rect CalculateTargetsBoundingBox()
     {
         float minX = Mathf.Infinity;
         float maxX = Mathf.NegativeInfinity;
         float minY = Mathf.Infinity;
         float maxY = Mathf.NegativeInfinity;
 
         foreach (Transform target in targets) {
             Vector3 position = target.position;
 
             minX = Mathf.Min(minX, position.x);
             minY = Mathf.Min(minY, position.y);
             maxX = Mathf.Max(maxX, position.x);
             maxY = Mathf.Max(maxY, position.y);
         }
 
         return Rect.MinMaxRect(minX - boundingBoxPadding, maxY + boundingBoxPadding, maxX + boundingBoxPadding, minY - boundingBoxPadding);
     }
 
     /// <summary>
     /// Calculates a camera position given the a bounding box containing all the targets.
     /// </summary>
     /// <param name="boundingBox">A Rect bounding box containg all targets.</param>
     /// <returns>A Vector3 in the center of the bounding box.</returns>
     Vector3 CalculateCameraPosition(Rect boundingBox)
     {
         Vector2 boundingBoxCenter = boundingBox.center;
 
         return new Vector3(boundingBoxCenter.x, boundingBoxCenter.y, -10f);
     }
 
     /// <summary>
     /// Calculates a new orthographic size for the camera based on the target bounding box.
     /// </summary>
     /// <param name="boundingBox">A Rect bounding box containg all targets.</param>
     /// <returns>A float for the orthographic size.</returns>
     float CalculateOrthographicSize(Rect boundingBox)
     {
         float orthographicSize = camera.orthographicSize;
         Vector3 topRight = new Vector3(boundingBox.x + boundingBox.width, boundingBox.y, 0f);
         Vector3 topRightAsViewport = camera.WorldToViewportPoint(topRight);
        
         if (topRightAsViewport.x >= topRightAsViewport.y)
             orthographicSize = Mathf.Abs(boundingBox.width) / camera.aspect / 2f;
         else
             orthographicSize = Mathf.Abs(boundingBox.height) / 2f;
 
         return Mathf.Clamp(Mathf.Lerp(camera.orthographicSize, orthographicSize, Time.deltaTime * zoomSpeed), minimumOrthographicSize, Mathf.Infinity);
     }
 }


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

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

Implementing Camera bounds proportionally to its ortographic size 0 Answers

Keeping the camera in bounds 1 Answer

How do I keep an Ortho camera in a specific range when the ortho changes? 3 Answers

Trying to use an orthographic camera in interior scenes 0 Answers

map boundaries for perspective camera 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