• 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
0
Question by tankhedgehog · Mar 31, 2018 at 01:50 AM · camerazoomscroll

How can I make zoom dependent camera constraints?

I'm working on a 2D project where the player can scroll the camera by clicking and dragging the middle mouse. They can also zoom in and out with the scroll wheel. The problem is that I want to stop my players from ever seeing off the map into the infinite blackness of the void, no matter what zoom level they are at.

Here's what I have currently:

 public class CameraController : MonoBehaviour
 {
     private Vector3 Origin;
     private Vector3 Diference;
     private bool Drag = false;
     private static bool cameraExists;
 
     // Use this for initialization
     void Start ()
     {
         if (!cameraExists)
         {
             cameraExists = true;
             DontDestroyOnLoad(transform.gameObject);
         }
         else
         {
             Destroy(gameObject);
         }
     }
     
     // Update is called once per frame
     void Update ()
     {
         
     }
 
     void LateUpdate()
     {
         if (Input.GetMouseButton(2))
         {
             Diference = (Camera.main.ScreenToWorldPoint(Input.mousePosition)) - Camera.main.transform.position;
             if (Drag == false)
             {
                 Drag = true;
                 Origin = Camera.main.ScreenToWorldPoint(Input.mousePosition);
             }
         }
         else
         {
             Drag = false;
         }
         if (Drag == true)
         {
             Camera.main.transform.position = Origin - Diference;
         }
 
         if (Input.GetAxis("Mouse ScrollWheel") < 0) // zoom out
         {
             Camera.main.orthographicSize = Camera.main.orthographicSize * 1.1f;
 
 
         }
         if (Input.GetAxis("Mouse ScrollWheel") > 0) // zoom in
         {
             Camera.main.orthographicSize = Camera.main.orthographicSize * 0.9f;
         }
 
     }
 }

Being a coding noob, most of this code is stuff that I found online and tweeked a little (original code let you zoom into the negative which caused the camera to flip upside-down)

Comment
Add comment · Show 1
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 tankhedgehog · Apr 03, 2018 at 12:28 AM 0
Share

In case anyone else has a similar problem, here's what I managed to come up with. It's an ugly mess and it's a little jittery when you try to zoom out too far, but it does the job for now. Please, if any actually experienced coders out there have any advice to improve the code, I'd really like to hear it.

 if (Input.GetAxis("$$anonymous$$ouse ScrollWheel") < 0) // zoom out
         {
             Camera.main.orthographicSize = Camera.main.orthographicSize * 1.1f;
         }
 
         //checking if zoomed out too far
         if ((Camera.main.ScreenToWorldPoint(new Vector2(0, 0)).x <= 0 && Camera.main.ScreenToWorldPoint(new Vector2(Camera.main.scaledPixelWidth, 0)).x >= 65) || (Camera.main.ScreenToWorldPoint(new Vector2(0, 0)).y <= 0 && Camera.main.ScreenToWorldPoint(new Vector2(0, Camera.main.scaledPixelHeight)).y >= 51))
         {
             Camera.main.orthographicSize = Camera.main.orthographicSize * 0.9f;
         }
 
         if (Input.GetAxis("$$anonymous$$ouse ScrollWheel") > 0) // zoom in
         {
             Camera.main.orthographicSize = Camera.main.orthographicSize * 0.9f;
         }
 
         //checking left
         if (Camera.main.ScreenToWorldPoint(new Vector2(0, 0)).x < 0)
         {
             Camera.main.transform.position = new Vector3(Camera.main.transform.position.x + -Camera.main.ScreenToWorldPoint(new Vector2(0, 0)).x, Camera.main.transform.position.y, Camera.main.transform.position.z);
         }
 
         //checking bottom
         if (Camera.main.ScreenToWorldPoint(new Vector2(0, 0)).y < 0)
         {
             Camera.main.transform.position = new Vector3(Camera.main.transform.position.x, Camera.main.transform.position.y + -Camera.main.ScreenToWorldPoint(new Vector2(0, 0)).y, Camera.main.transform.position.z);
         }
         
         //checking right
         if (Camera.main.ScreenToWorldPoint(new Vector2(Camera.main.scaledPixelWidth, 0)).x > 65)
         {
             Camera.main.transform.position = new Vector3(Camera.main.transform.position.x - (Camera.main.ScreenToWorldPoint(new Vector2(Camera.main.scaledPixelWidth, 0)).x - 65), Camera.main.transform.position.y, Camera.main.transform.position.z);
         }
 
         //checking top
         if (Camera.main.ScreenToWorldPoint(new Vector2(0, Camera.main.scaledPixelHeight)).y > 51)
         {
             Camera.main.transform.position = new Vector3(Camera.main.transform.position.x, Camera.main.transform.position.y - (Camera.main.ScreenToWorldPoint(new Vector2(0, Camera.main.scaledPixelHeight)).y - 51), Camera.main.transform.position.z);
         }

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by YBtheS · Apr 05, 2018 at 09:58 PM

You can have a collider (or multiple) lining the edge of the map and another on the GameObject that your camera is attached to that scales with the orthographic size. I believe that would look like this:

 void Update() {
     Camera.main.gameObject.transform.localScale = Vector3.one * Camera.main.orthographicSize;
 }

This would make your camera collide with the edge of the map thus stopping its motion. Make sure that you add a non-kinematic rigidbody to your camera's GameObject. I hope that that helps!

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.

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

125 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

Related Questions

How do I make the camera zoom in and out with the mouse wheel? 10 Answers

Can you use UI Scroll Rect for camera movement? 1 Answer

Need help implementing a zoom function 0 Answers

Implementing Camera bounds proportionally to its ortographic size 0 Answers

How do you zoom with an orthographic camera? 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