Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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
0
Question by SBull · Sep 02, 2014 at 05:22 PM · camerajavascriptlerpzoomclamp

Clamping a 2D camera while zooming.

I am making a 2D game where the player is able to move the camera around by clicking and dragging with the mouse as well as zoom in and out using the mouse wheel. I would like to make it so that player cannot see outside the edges of the gameplay area no matter how zoomed they are. In order to do this, I have tried setting it up so that if the player is zoomed all the way out, they will not be able to move the camera at all, but the further the camera is zoomed in, the move they can move the camera.

However, I my lerp setup doesn't seem to be working the way I intended it to and I'm not sure where my error is. It currently does not change the value of the clamp at all and just sets the clamp to whatever my cameraMin and cameraMax values are.

So I am looking for why my code doesn't seem to function as intended or if anyone knows a better way to go about this issue. Here is the code I have as of now:

 public var orthoZoomSpeed : float = 1f; // The rate of change of the orthographic size in orthographic mode.
 var cameraMin : float;
 var cameraMax : float;
 var dragOrigin : Vector3;
 var dragSpeed : float;
 private var timeDragStarted : float;
 var previousPosition : Vector3;
 
 function Update ()
 {
 if (Input.GetMouseButtonDown(0))
         {
             //Drag start
             timeDragStarted = Time.time;
             dragSpeed = 0f;
             previousPosition = Input.mousePosition;
         }
 
         //Calculate time difference in order for the following code NOT to run on single taps
         else if (Input.GetMouseButton(0) && Time.time - timeDragStarted > 0.05f)
         {
             //Find the delta of this point with the previous frame
             var input = Input.mousePosition;
             var deltaX = (previousPosition.x - input.x) * dragSpeed;
             var deltaY = (previousPosition.y - input.y) * dragSpeed;


 **//THIS IS THE LERP/CLAMP SECTION I AM HAVING TROUBLE WITH///////
 
             //Sets variables to deterine how far the camera is zoomed which 
             //interpolates the distance it is clamped on the x and y axis
             var currentOrth : float = camera.orthographicSize;
             var clampMinX : float = Mathf.Lerp(cameraMax, cameraMin, currentOrth);
             var clampMaxX : float = Mathf.Lerp(cameraMin, cameraMax, currentOrth);
             var clampMinY : float = Mathf.Lerp(cameraMax, cameraMin, currentOrth);
             var clampMaxY : float = Mathf.Lerp(cameraMin, cameraMax, currentOrth);
 
             //Clamp the values to limit drag
             var newX = Mathf.Clamp(transform.position.x + deltaX, clampMinX, clampMaxX);
             var newY = Mathf.Clamp(transform.position.y + deltaY, clampMinY, clampMaxY);**
         
             //Move Camera
             transform.position = new Vector3(newX, newY, transform.position.z);
         
             previousPosition = input;
         
             //Some small acceleration
             if(dragSpeed < 0.1f) dragSpeed += 0.2f;
         }
     }
 
        //ScrollWheel to zoom in and out.
        if( Input.GetAxis("Mouse ScrollWheel") > 0 && camera.orthographicSize > cameraMin)
        {
         var mousePos = camera.ScreenToWorldPoint(Vector3 (Input.mousePosition.x, Input.mousePosition.y, camera.transform.position.z));
         var mouseOrigin = camera.ScreenToWorldPoint(Vector3 (Screen.width / 2, Screen.height / 2, camera.transform.position.z));
         transform.position.y = transform.position.y + ((mousePos.y - mouseOrigin.y) / (camera.orthographicSize * 2));
         transform.position.x = transform.position.x + ((mousePos.x - mouseOrigin.x) / (camera.orthographicSize * 2));
         transform.position.z = -15;
 
         camera.orthographicSize -= .5 * orthoZoomSpeed;
         camera.orthographicSize = Mathf.Clamp(camera.orthographicSize, cameraMin, cameraMax);
     }
 
     else if(Input.GetAxis("Mouse ScrollWheel") < 0 && camera.orthographicSize < cameraMax)
     {
         mousePos = camera.ScreenToWorldPoint(Vector3 (Input.mousePosition.x, Input.mousePosition.y, camera.transform.position.z));
         mouseOrigin = camera.ScreenToWorldPoint(Vector3 (Screen.width / 2, Screen.height / 2, camera.transform.position.z));
         transform.position.y = transform.position.y - ((mousePos.y - mouseOrigin.y) / (camera.orthographicSize * 2));
         transform.position.x = transform.position.x - ((mousePos.x - mouseOrigin.x) / (camera.orthographicSize * 2));
         transform.position.z = -15;
         
         camera.orthographicSize += .5 * orthoZoomSpeed;
         camera.orthographicSize = Mathf.Clamp(camera.orthographicSize, cameraMin, cameraMax);
     }
     
     mousePos = camera.ScreenToWorldPoint(Vector3 (Input.mousePosition.x, Input.mousePosition.y, 0 - camera.transform.position.z));
     mouseOrigin = camera.transform.position;
 }

Thanks for any help!

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

0 Replies

· Add your reply
  • Sort: 

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

22 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

Related Questions

Zooming camera using move z position. 0 Answers

camera zoom 1 Answer

How to zoom in smoothly with orthographic camera? 1 Answer

How to zoom camera in x amount of seconds 1 Answer

Camera zoom smoothing 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