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
1
Question by niall111 · Jul 11, 2011 at 05:07 PM · camera2dorthographic

Orthographic camera movement clamping

I have an orthographic camera that is dynamically sized to give me pinch/zoom effects on a large background map. The camera's position is also adjusted as you drag the map around.

What I need to do is prevent the camera from moving outside of our predetermined map area. Initially I thought I could just clamp the camera position, but the value that you need to clamp to is dependent on the camera's current orthographicSize.

So the question is, how can I determine the current "bounding rectangle"(??) for an orthographic camera in world space? At first I thought it was simply the current camera location with the ortho size as an offset on all 4 sides, but that doesn't seem to be accurate.

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 Chris D · Jul 11, 2011 at 05:59 PM 4
Share

Looking at the camera docs, it seems the orthographicSize gives you the only the vertical half-size and that the horizontal changes with aspect ratio. Is that what you've already accounted for or were you applying the orthoSize to both width and height?

avatar image niall111 · Jul 11, 2011 at 06:20 PM 0
Share

aha! I was only looking horizontally, but you are correct! The horizonal width is dependent on the aspect ratio as well, but can at least be calculated now. Thanks.

avatar image ritual · Mar 14, 2014 at 07:31 PM 0
Share

i'm having the same issue. were you able to figure it out.?

im still kinda confused

2 Replies

· Add your reply
  • Sort: 
avatar image
1

Answer by Simeon2020 · Apr 02, 2014 at 02:06 PM

Adapted from this answer (and converted to c#), main difference for mine is that extents are moved into update to allow dynamic bounds with pinch and zoom: http://answers.unity3d.com/questions/501893/calculating-2d-camera-bounds.html

  public class Camera_Bounds : MonoBehaviour 
     {
         //mapX, mapY is size of background image
         private float mapX = 61.4f; 
         private float mapY = 41.4f;
         private float minX;
         private float maxX;
         private float minY;
         private float maxY;
     
         void Update()
            {
             vertExtent = Camera.main.camera.orthographicSize;  
             horzExtent = vertExtent * Screen.width / Screen.height;
             
         // Calculations assume map is position at the origin
             minX = horzExtent - mapX / 2.0f;
             maxX = mapX / 2.0f - horzExtent ;
             minY = vertExtent - mapY / 2.0f;
             maxY = mapY / 2.0f - vertExtent;
         }
     
         void LateUpdate() 
         {
             Vector3 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 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 JPBA1984 · May 01, 2017 at 06:09 AM 0
Share

what would vertExtent and horzExtent be? private floats? thanks.

avatar image
0

Answer by patrik-org · Apr 02, 2014 at 05:05 PM

Below is the way I solved it in my project. The orthographical camera is set up so that it covers the entire background image at start. I use a reference camera (copy of the camera) that is set to not render anything. I use the reference camera to get unzoomed & unpanned size as well as getting correct focus point depending on view target position.

 void LateUpdate()
 {
     // get camera offset
     var offset = (_referenceCamera.WorldToViewportPoint(_cameraFocus.transform.position) - new Vector3(0.5f, 0.5f, 0.0f));
 
     // clamp offset depending on zoom
     float clamp = 0.5f - 0.5f / _cameraZoom;
     offset = new Vector3(Mathf.Clamp(offset.x, -clamp, clamp), Mathf.Clamp(offset.y, -clamp, clamp), offset.z);
 
     // pan camera
     var position = _referenceCamera.transform.position + new Vector3(offset.x * _referenceCamera.orthographicSize * 2.0f * camera.aspect, offset.y * _referenceCamera.orthographicSize * 2.0f, 0f);
     camera.transform.position = position; 
 
     // zoom camera
     camera.orthographicSize = _referenceCamera.orthographicSize / _cameraZoom;
 }
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

6 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

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

Orthographic Camera settings for a 2D game 1 Answer

Iphone 2d camera settings 1 Answer

Making a camera semi-orthographic 2 Answers

2D Camera sizing and movement 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