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

Answer by SvGampel · Mar 22, 2015 at 12:16 AM

 using UnityEngine;
 
 public static class Extensions
 {
     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();
         }
 
         var t = camera.transform;
         var x = t.position.x;
         var y = t.position.y;
         var size = camera.orthographicSize * 2;
         var width = size * (float)Screen.width / Screen.height;
         var height = size;
 
         return new Bounds(new Vector3(x, y, 0), new Vector3(width, height, 0));
     }
 }
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
21

Answer by GeekyMonkey · Aug 28, 2014 at 02:54 AM

This is my Camera extension class function to get the orthographic camera bounds in world space:

 public static class CameraExtensions
 {
     public static Bounds OrthographicBounds(this Camera camera)
     {
         float screenAspect = (float)Screen.width / (float)Screen.height;
         float cameraHeight = camera.orthographicSize * 2;
         Bounds bounds = new Bounds(
             camera.transform.position,
             new Vector3(cameraHeight * screenAspect, cameraHeight, 0));
         return bounds;
     }
 }



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 khalidse · Dec 26, 2015 at 05:45 PM 0
Share

thanks!...

avatar image Vivien_Lynn · Oct 28, 2020 at 02:39 PM 0
Share

I use Pillarboxes to maintain an AspectRatio of 9/16. So I had to adjust your solution slightly. Instead of calculating the Bound-Width with cameraHeight * screenAspect I have to make sure it always has the right width: (width - (width x 2 x CameraRect.x )). So the x-Value of my new Vector3 is (cameraHeight * screenAspect) - (cameraHeight * screenAspect * 2f * cam.rect.x);

avatar image
5

Answer by JacobWMills · Jun 21, 2014 at 01:47 PM

In case anyone else finds this answer on Google like I did, I wrote a little extension script for the Camera class that adds bounds-like functionality to cameras in general:

 public static class CameraExtensions
 {
     //******Orthographic Camera Only******//
     
     public static Vector2 BoundsMin(this Camera camera)
     {
         return camera.transform.position.ToVector2() - camera.Extents();
     }
     
     public static Vector2 BoundsMax(this Camera camera)
     {
         return camera.transform.position.ToVector2() + camera.Extents();
     }
     
     public static Vector2 Extents(this Camera camera)
     {
         if (camera.orthographic)
             return new Vector2(camera.orthographicSize * Screen.width/Screen.height, camera.orthographicSize);
         else
         {
             Debug.LogError("Camera is not orthographic!", camera);
             return new Vector2();
         }
     }
     //*****End of Orthographic Only*****//
 }
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 keburanuil · Jul 08, 2015 at 05:23 PM 0
Share

ToVector2() no longer works in Unity 5. Use the Vector2-cast:

 return (Vector2)camera.transform.position - camera.Extents();
  • ‹
  • 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