Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Ortho Camera Click and Drag + Zoom with bounds

Discussion in 'Scripting' started by Deleted User, Jun 10, 2012.

  1. Deleted User

    Deleted User

    Guest

    My camera is looking at a plane with a map texture on it. What I would like to do is be able to zoom into the map (with the mouse wheel). When zoomed in, the user can click and drag to move around the map. Here's what I cannot figure out: 1) I want to zoom towards the mouse cursor (like Google Maps). 2) I want to lock the camera such that it never shows the "out-of bounds" area. Since the zoom can be anywhere between 100% - 150% the camera will lock at different places at each zoom level. Part of this would be as they zoom out, the camera will recenter so as to not show the dead space.

    Can someone point me in the right direction (C#)? Thanks.
     
    Last edited by a moderator: Jun 10, 2012
  2. Deleted User

    Deleted User

    Guest

    Finally figured it out. Code below in case anyone else happens upon this post. Probably not the most efficient way, but it works. (Working on the zoom to cursor still, will update when I get it working)

    Code (csharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class MapControl : MonoBehaviour {
    5.     public float maxZoom;
    6.     public float minZoom;
    7.     public float panSpeed = -1;
    8.    
    9.     Vector3 bottomLeft;
    10.     Vector3 topRight;
    11.    
    12.     float cameraMaxY;
    13.     float cameraMinY;
    14.     float cameraMaxX;
    15.     float cameraMinX;
    16.    
    17.     void Start()
    18.     {  
    19.         //set max camera bounds (assumes camera is max zoom and centered on Start)
    20.         topRight = camera.ScreenToWorldPoint(new Vector3(camera.pixelWidth, camera.pixelHeight, -transform.position.z));
    21.         bottomLeft = camera.ScreenToWorldPoint(new Vector3(0,0,-transform.position.z));
    22.         cameraMaxX = topRight.x;
    23.         cameraMaxY = topRight.y;
    24.         cameraMinX = bottomLeft.x;
    25.         cameraMinY = bottomLeft.y;
    26.     }
    27.    
    28.     void Update ()
    29.     {
    30.         //click and drag
    31.         if(Input.GetMouseButton(0))
    32.         {
    33.             float x = Input.GetAxis("Mouse X") * panSpeed;
    34.             float y = Input.GetAxis("Mouse Y") * panSpeed;
    35.             transform.Translate(x,y,0);
    36.         }
    37.        
    38.         //zoom
    39.         if((Input.GetAxis("Mouse ScrollWheel") > 0)  Camera.main.orthographicSize > minZoom ) // forward
    40.         {
    41.             Camera.main.orthographicSize = Camera.main.orthographicSize - 0.5f;
    42.         }
    43.            
    44.         if ((Input.GetAxis("Mouse ScrollWheel") < 0)  Camera.main.orthographicSize < maxZoom) // back            
    45.         {
    46.             Camera.main.orthographicSize = Camera.main.orthographicSize + 0.5f;
    47.         }
    48.        
    49.        
    50.         //check if camera is out-of-bounds, if so, move back in-bounds
    51.         topRight = camera.ScreenToWorldPoint(new Vector3(camera.pixelWidth, camera.pixelHeight, -transform.position.z));
    52.         bottomLeft = camera.ScreenToWorldPoint(new Vector3(0,0,-transform.position.z));
    53.        
    54.         if(topRight.x > cameraMaxX)
    55.         {
    56.             transform.position = new Vector3(transform.position.x - (topRight.x - cameraMaxX), transform.position.y, transform.position.z);
    57.         }
    58.        
    59.         if(topRight.y > cameraMaxY)
    60.         {
    61.             transform.position = new Vector3(transform.position.x, transform.position.y - (topRight.y - cameraMaxY), transform.position.z);
    62.         }
    63.        
    64.         if(bottomLeft.x < cameraMinX)
    65.         {
    66.             transform.position = new Vector3(transform.position.x + (cameraMinX - bottomLeft.x), transform.position.y, transform.position.z);
    67.         }
    68.        
    69.         if(bottomLeft.y < cameraMinY)
    70.         {
    71.             transform.position = new Vector3(transform.position.x, transform.position.y + (cameraMinY - bottomLeft.y), transform.position.z);
    72.         }
    73.     }
    74. }
    75.  
     
    Last edited by a moderator: Jun 12, 2012
    washagames likes this.
  3. aorata

    aorata

    Joined:
    Feb 10, 2012
    Posts:
    28
    Hey man, maybe its not the most efficient, but its simple and it works well! thanks buddy
     
  4. gmilitel

    gmilitel

    Joined:
    Oct 14, 2014
    Posts:
    20
    Thanks for the script I'd used on my project but I received this error

    Assets/Standard Assets/Character Controllers/MapControl.cs(39,67): error CS1525: Unexpected symbol `Camera'

    Is there something wrong in the code?

    Please help!

    Thanks.
     
  5. tomergt45

    tomergt45

    Joined:
    Nov 16, 2016
    Posts:
    1
    Hi Dud I get the same error , just replace "Camera" for
    • GetComponent<Camera>().
    GL :)