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

Camera pan and zoom

Discussion in 'Scripting' started by gmilitel, Apr 14, 2015.

  1. gmilitel

    gmilitel

    Joined:
    Oct 14, 2014
    Posts:
    20
    Hi, can someone help me with a script for move+zoom top view ortho camera with mouse click like google maps?

    Thanks.
     
  2. GroZZleR

    GroZZleR

    Joined:
    Feb 1, 2015
    Posts:
    3,201
    What do you have so far? What aspects are you struggling with?
     
    jtsmith1287 likes this.
  3. gmilitel

    gmilitel

    Joined:
    Oct 14, 2014
    Posts:
    20
    I found in this post the script below

    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.     }[/SIZE]
    75.  
    76.  
    but gives the error:

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

    I have a simple terrain with a camera with their X axis at 90 so the view is similar to a map and I wanted to pan and zoom the camera to resemble google maps like camera.

    I found StrategyCam in the asset store too that emulates what I'm trying to achieve, but is not free.

    Regards.
     
  4. A.Killingbeck

    A.Killingbeck

    Joined:
    Feb 21, 2014
    Posts:
    483
    I believe all common properties were removed in Unity5. At the beginning of your Start function, put this:

    Code (CSharp):
    1. var camera = GetComponent<Camera>();
     
  5. gmilitel

    gmilitel

    Joined:
    Oct 14, 2014
    Posts:
    20
    Thanks, I'll try it as soon as I can, I'm using Unity 4.6.1f1

    Regards
     
  6. A.Killingbeck

    A.Killingbeck

    Joined:
    Feb 21, 2014
    Posts:
    483
    Actually, the problem is probably there is no camera in your scene tagged as "Main Camera". Make sure of that before doing anything else
     
  7. gmilitel

    gmilitel

    Joined:
    Oct 14, 2014
    Posts:
    20
    I have a Main Camera on my project, actually If I comment the zoom code

    Code (CSharp):
    1.         /*//zoom
    2.         if((Input.GetAxis("Mouse ScrollWheel") > 0) Camera.main.orthographicSize > minZoom ) // forward
    3.         {
    4.             Camera.main.orthographicSize = Camera.main.orthographicSize - 0.5f;
    5.         }
    6.        
    7.         if ((Input.GetAxis("Mouse ScrollWheel") < 0) Camera.main.orthographicSize < maxZoom) // back          
    8.         {
    9.             Camera.main.orthographicSize = Camera.main.orthographicSize + 0.5f;
    10.         }
    11.         */
    12.  
    The script compile...but nothing is done when I press play.
     
  8. A.Killingbeck

    A.Killingbeck

    Joined:
    Feb 21, 2014
    Posts:
    483
    Sorry, I've been blind:

    Code (CSharp):
    1.  
    2.  if(Input.GetAxis("Mouse ScrollWheel") > 0 &&  Camera.main.orthographicSize > minZoom ) // forward
    3. {
    4.  
    5. }
    6.        
    Your if statement was badly written. You need && or || operators when combining conditional statements.
     
    gmilitel likes this.
  9. gmilitel

    gmilitel

    Joined:
    Oct 14, 2014
    Posts:
    20
    Great!! they compile now..thanks for the tip, now when I press play only zoom in is allowed with mouse wheel, there is no zoom out ...
     
  10. A.Killingbeck

    A.Killingbeck

    Joined:
    Feb 21, 2014
    Posts:
    483
    Have you made the same syntax changes to your other if statement?
     
  11. gmilitel

    gmilitel

    Joined:
    Oct 14, 2014
    Posts:
    20
    Yes...my fault, there are three parameters the script has that need to be changed in the Inspector window:

    Max zoom:
    Min zoom:
    Pan Speed:

    all was in zero, so changed and seems to be working now..
     
  12. gmilitel

    gmilitel

    Joined:
    Oct 14, 2014
    Posts:
    20
    Thanks Andrew, your help is very appreciated.!
     
  13. gmilitel

    gmilitel

    Joined:
    Oct 14, 2014
    Posts:
    20
    Can I ask you another question? How can I make zoom go faster?
     
  14. Deleted User

    Deleted User

    Guest

    @gmilitel Camera.main.orthographicSize = Camera.main.orthographicSize + 0.5f; change the .5 to something bigger.

    hey does anyone know how to modify the code gmilitel posted to work on x and z axis instead of x and y?
     
  15. Deleted User

    Deleted User

    Guest

    For anyone that has the same problem.
    I change around a little code and just set the y in the transform to a set value works great for a 2D map.
    Code (CSharp):
    1.  
    2. using UnityEngine;
    3. using System.Collections;
    4.  
    5. public class MapCamera : MonoBehaviour {
    6.  
    7.     private Camera MapCam;
    8.  
    9.     public float maxZoom;
    10.     public float minZoom;
    11.     public float panSpeed = -1;
    12.  
    13.     Vector3 bottomLeft;
    14.     Vector3 topRight;
    15.  
    16.     float cameraMaxY;
    17.     float cameraMinY;
    18.     float cameraMaxX;
    19.     float cameraMinX;
    20.     bool CamSet = false;
    21.  
    22.     void Start()
    23.     {
    24.         MapCam = this.gameObject.GetComponent<Camera>();
    25.         //set max camera bounds (assumes camera is max zoom and centered on Start)
    26.         topRight = MapCam.ScreenToWorldPoint(new Vector3(MapCam.pixelWidth, MapCam.pixelHeight, -transform.position.y));
    27.         bottomLeft = MapCam.ScreenToWorldPoint(new Vector3(0,0,-transform.position.y));
    28.         cameraMaxX = topRight.x;
    29.         cameraMaxY = topRight.y;
    30.         cameraMinX = bottomLeft.x;
    31.         cameraMinY = bottomLeft.y;
    32.     }
    33.  
    34.     void Update () {
    35.  
    36.         if(Input.GetMouseButton(2))
    37.         {
    38.             float x = Input.GetAxis("Mouse X") * panSpeed;
    39.             float y = Input.GetAxis("Mouse Y") * panSpeed;
    40.             transform.Translate(x, 0, y);
    41.         }
    42.  
    43.         if (Input.GetAxis("Mouse ScrollWheel") < 0) //Back
    44.         {
    45.             if(MapCam.orthographicSize < 95)
    46.                 MapCam.orthographicSize = MapCam.orthographicSize + 4;
    47.         }
    48.         if (Input.GetAxis("Mouse ScrollWheel") > 0) // Forward
    49.         {
    50.             if(MapCam.orthographicSize > 10)
    51.                 MapCam.orthographicSize = MapCam.orthographicSize - 4;
    52.         }
    53.  
    54.             topRight = MapCam.ScreenToWorldPoint(new Vector3(MapCam.pixelWidth, MapCam.pixelHeight, -transform.position.y));
    55.             bottomLeft = MapCam.ScreenToWorldPoint(new Vector3(0,0,-transform.position.y));
    56.        
    57.             if(topRight.x > cameraMaxX)
    58.             {
    59.                 transform.position = new Vector3(transform.position.x - (topRight.x - cameraMaxX), 165, transform.position.z);
    60.             }
    61.        
    62.             if(topRight.y > cameraMaxY)
    63.             {
    64.                 transform.position = new Vector3(transform.position.x, 165, transform.position.z - (topRight.y - cameraMaxY));
    65.             }
    66.        
    67.             if(bottomLeft.x < cameraMinX)
    68.             {
    69.                 transform.position = new Vector3(transform.position.x + (cameraMinX - bottomLeft.x), 165, transform.position.z);
    70.             }
    71.        
    72.             if(bottomLeft.y < cameraMinY)
    73.             {
    74.                 transform.position = new Vector3(transform.position.x, 165, transform.position.z + (cameraMinY - bottomLeft.y));
    75.            }
    76.        }
    77.    }
    78.  
    So after a little more testing it turns out this code works perfect for x movement but for the z movement it lets you move infinitely to either side. I don't know what I'm doing wrong, a little help plz?
     
    Last edited by a moderator: Jul 18, 2015