• 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 /
This post has been wikified, any user with enough reputation can edit it.
avatar image
18
Question by rolandshoemaker · Jul 27, 2013 at 08:51 AM · camera2dorthographic

Calculating 2D camera bounds

I'm writing a very simple top down game, I am using an orthographic camera looking at a 1x1 cube that will represent the player and a 100x100 cube that the map is put on.

I am currently working on bonding the camera so that it cannot pass the edge of the cube on which the map is textured. I looked into using WorldtoScreenPoint to find where the edges of the 100x100 cube were but they didn't really make sense, I assume since it's 100x100 positioned at 0,0 it should be 50,50 and -50,-50 but I always get weird divisions. (I am using an orthographic camera that is 1 unit away from map/player.

Is it possible to calculate where the borders of the cube should/are so that I can bound the camera within them?

Comment
Add comment · Show 3
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 RPGstandard · Jul 27, 2013 at 09:42 AM 0
Share

what are the figures that its giving you?

avatar image rolandshoemaker RPGstandard · Jul 27, 2013 at 10:12 AM 0
Share
 Debug.Log(Camera.main.WorldToScreenPoint(GameObject.Find("map").transform.position));

returns (2485.4, -1793.1, 1.0) at the top left conrner

avatar image RPGstandard RPGstandard · Jul 27, 2013 at 10:40 AM 0
Share

in the transform of the cube is the position set to (0,0,0)?

8 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by MorphVGX · Aug 03, 2016 at 09:49 PM

This is what worked for me to get the limits for the camera position:

 float camExtentV = _cam.orthographicSize;  
 float camExtentH = (camExtentV * Screen.width) / Screen.height;
 
 Bounds levelBounds = LevelBoundsSpriteRenderer.bounds;
 
 float leftBound = levelBounds.min.x + camExtentH;
 float rightBound = levelBounds.max.x - camExtentH;
 float bottomBound = levelBounds.min.y + camExtentV;
 float topBound = levelBounds.max.y - camExtentV;    


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
avatar image
0

Answer by sehathesam · Jun 25, 2019 at 01:30 PM

Hi everyone my solution is :

 public class Utility
 {
     public static Bounds OrthographicBounds( Camera camera)
     {
         float cameraHeight = camera.orthographicSize * 2;
         Bounds bounds = new Bounds(
             camera.transform.position,
             new Vector3(cameraHeight * camera.aspect, cameraHeight, 0));
         return bounds;
     }
 }
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
avatar image
0

Answer by RChrispy · Sep 23, 2019 at 04:58 PM

Since this will give different results in "game screen" and "editor screen" I took @SvGampel 's answer and modified it a little:

     /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
     /********************
      * CAMERA  *
      ********************/
     /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
     /// <summary>
     /// Returns the orthographic camera bounds
     /// </summary>
     /// <param name="_Camera"></param>
     /// <returns></returns>
     public static Bounds OrthographicBounds(this Camera _Camera)
     {
         if (!_Camera.orthographic)
         {
             Debug.Log(string.Format("The camera {0} is not Orthographic!", _Camera.name), _Camera);
             return new Bounds();
         }
 
         Transform camTrans = _Camera.transform;
         float scaleWidthFactor = (float)Screen.currentResolution.width / (float)Screen.currentResolution.height;
         float height = _Camera.orthographicSize * 2f;
         float width = height * scaleWidthFactor;
         float viewDepth = _Camera.farClipPlane - _Camera.nearClipPlane;
 
         return new Bounds(new Vector3(camTrans.position.x, camTrans.position.y, camTrans.position.z), new Vector3(width, height, viewDepth));
     }
 
     /// <summary>
     /// Returns the orthographic camera rect (without z)
     /// </summary>
     /// <param name="_Camera"></param>
     /// <returns></returns>
     public static Rect OrthographicRect(this Camera _Camera)
     {
         if (!_Camera.orthographic)
         {
             Debug.Log(string.Format("The camera {0} is not Orthographic!", _Camera.name), _Camera);
             return new Rect();
         }
 
         Transform camTrans = _Camera.transform;
         float scaleWidthFactor = (float)Screen.currentResolution.width / (float)Screen.currentResolution.height;
         float height = _Camera.orthographicSize * 2f;
         float width = height * scaleWidthFactor;
 
         return new Rect(new Vector2(camTrans.position.x - (width*0.5f), camTrans.position.y - (height * 0.5f)), new Vector2(width, height));
     }

This code will always give the set resolution. (float)Screen.currentResolution.width instead of (float)Screen.width .

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
  • ‹
  • 1
  • 2

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

30 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 avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Orthographic camera movement clamping 1 Answer

Making a camera semi-orthographic 2 Answers

Dynamic Orthagraphic Camera Zoom 1 Answer

Resizing orthographic camera to fit 2d sprite on screen 1 Answer

How can I find out a ViewportToWorldPoint position with a different orthographic 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