Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 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 /
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
49
Best Answer

Answer by robertbu · Jul 27, 2013 at 07:34 PM

WorldToScreenPoint() is not what you need to solve this problem. Screen coordinates are pixels on the screen starting at 0,0 in the lower left. So your call above took the center of the cube and translated it into a screen position. The fractional amounts are due the fact that the real-world coordinates of your cube rarely land exactly on a pixel. Judging by the numbers returned, your camera was off in the corner of your map.

So to solve your specific problem, take a look at Camera.orthographicSize. It is half the vertical size seen by the camera. So if you have a cube that is 1x1x1 with your camera centered on it, you could set your orthographicSize to 0.5, and vertically it would just fit on the screen. The distance between the camera and the cube does not matter. The resolution of the screen you are playing on does not matter. If you set orthographicSize to 50(and centered the camera), then vertically your map would just fit the camera. We can calculate how much the camera sees horizontally by:

 horzExtent = Camera.main.ortiographicSize * Screen.width / Screen.height;

'horzExtent' will be 1/2 of the horizontal size the camera sees in World coordinates.

So if you want to limit your camera to seeing just your map you can do something like this to your camera script:

 #pragma strict
 
     var mapX = 100.0;
     var mapY = 100.0;
     
     private var minX : float;
     private var maxX : float;
     private var minY : float;
     private var maxY : float;
     
     function Start() {
         var vertExtent = Camera.main.camera.orthographicSize;    
         var horzExtent = vertExtent * Screen.width / Screen.height;
 
         // Calculations assume map is position at the origin
         minX = horzExtent - mapX / 2.0;
         maxX = mapX / 2.0 - horzExtent;
         minY = vertExtent - mapY / 2.0;
         maxY = mapY / 2.0 - vertExtent;
     }
     
     function LateUpdate() {
         var v3 = transform.position;
         v3.x = Mathf.Clamp(v3.x, minX, maxX);
         v3.y = Mathf.Clamp(v3.y, minY, maxY);
         transform.position = v3;
     }
Comment
Add comment · Show 4 · 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 rolandshoemaker · Jul 28, 2013 at 06:32 AM 0
Share

Thanks! This is perfect, i didn't realize i could extrapolate between the orthographic camera size and the map co-ordinates... this is super useful!

avatar image zoran404 · Nov 07, 2015 at 02:59 PM 2
Share

You have a typo in your first line of code, it should be orthographicSize ins$$anonymous$$d of ortiographicSize

avatar image wind_flash · Sep 08, 2016 at 04:42 AM 0
Share

You are genius man,,, thats code really help me :) thx

avatar image Camenwolf · Mar 24, 2019 at 07:21 PM 1
Share

I love this community! 6 years after this comment was posted it is still helping people. This was a lifesaver. Or at least a phenomenal time saver!

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();
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
1

Answer by Magius96 · Oct 22, 2015 at 08:16 AM

I had a similiar issue lately. My solution was to get the world bounds from the screen, allowing it to adjust to resolution changes. Might not be the best solution, but it works for what I was doing. Note, the code below adds a 5 pixel buffer to the boundary, you can remove that.

 private Limits GetLimits()
 {
     var val=new Limits();
     var lowerLeft = Camera.main.ScreenToWorldPoint(new Vector3(0, 0, 0));
     var upperRight = Camera.main.ScreenToWorldPoint(new Vector3(Screen.width, Screen.height, 0));
     val.Left = lowerLeft.x + 5f;
     val.Right = upperRight.x - 5f;
     val.Top = upperRight.y - 5f;
     val.Bottom = lowerLeft.y + 5f;
     return val;
 }

 public class Limits
 {
     public float Left { get; set; }
     public float Right { get; set; }
     public float Top { get; set; }
     public float Bottom { get; set; }
 }
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.

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

Why is part of object outside camera frustum visible? 1 Answer

2d orthographic camera follow 1 Answer

Camera 2D RPG in perspective 1 Answer

Sprite relative position/scale problem with orthographic camera 0 Answers

ScreenToWorldPoint not accurate 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