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 /
  • Help Room /
avatar image
0
Question by serentixstudios · Jul 20, 2021 at 08:10 PM · cameracamera-movementcamera followcamera movementcamera viewport

Camera bounds with zoomable camera

I'm making a top-down 2D shooter and I have a camera control script that allows you to zoom in and out with the scroll wheel. But I need to add bounds to the camera. This is what I have at the moment.

 void CameraZoom()
     {
         zoom -= Input.GetAxis("Mouse ScrollWheel") * zoomSensitivity;
         zoom = Mathf.Clamp(zoom, zoomMin, zoomMax);
         cam.orthographicSize = Mathf.Lerp(cam.orthographicSize, zoom, Time.deltaTime * zoomSpeed);
         CalculateCameraBounds();
     }
 
     void CalculateCameraBounds()
     {
         vertBounds = cam.orthographicSize;
         horzBounds = vertBounds * cam.aspect;
 
         minX = horzBounds - mapX / 2.0f;
         maxX = mapX / 2.0f - horzBounds;
         minY = vertBounds - mapY / 2.0f;
         maxY = mapY / 2.0f - vertBounds;
     }
 
     void CameraFollow()
     {
         Vector3 targetPosition = new Vector3(target.position.x, target.position.y, transform.position.z);
 
         Vector3 boundPosition = new Vector3(
             Mathf.Clamp(targetPosition.x, maxX, minX),
             Mathf.Clamp(targetPosition.y, minY, maxY),
             transform.position.z);
 
         transform.position = Vector3.Lerp(transform.position, boundPosition, smoothing);
     }

This works for bounding the camera's Y position,. but doesn't for the X. It just makes the minX and maxX values larger, unlike the the minY and maxY values getting smaller like I want. I'm pretty sure I know the issue lies here:

 vertBounds = cam.orthographicSize;
 horzBounds = vertBounds * cam.aspect;

As I zoom the camera out, the orthographic size gets larger, which is then being multiplied by the aspect , thus making the horizontal bounds larger rather than smaller. I'm aware of this and have spent hours trying to figure out a way to change it up so that doesn't happen, but nothing comes to mind. Would appreciate any ideas or solutions.

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

1 Reply

· Add your reply
  • Sort: 
avatar image
0
Best Answer

Answer by serentixstudios · Jul 21, 2021 at 12:40 AM

After staring at this for several hours, I finally managed to figure this out and it was VERY simple.

 minX = horzBounds - mapX / 2.0f;
 maxX = mapX / 2.0f - horzBounds;

Here I was dividing the mapX by two. but what I should have been doing was MULTIPLYING it by 2.

 minX = horzBounds - mapX * 2.0f;
 maxX = mapX * 2.0f - horzBounds;

This change has made it work properly assuming I have the mapX set properly.

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

235 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 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 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 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 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 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 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 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

How to set movement limit to Camera in a top-down 3D game 0 Answers

Object follows AR Camera 1 Answer

I need help with my third person camera raycast 0 Answers

Camera flickers while chasing the player 0 Answers

Scripting beginner - how can I make the camera follow the character but still face the same direction? 2 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