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

Mobile camera script to rotate and pinch to zoom

Discussion in 'Scripting' started by schwooba, May 20, 2015.

  1. schwooba

    schwooba

    Joined:
    Nov 25, 2013
    Posts:
    33
    Hi everyone,

    Can some lend a hand to a noobie? I'm trying to modify this script to work on a mobile/touch screen. I want it to rotate and zoom using pinching. The rotating part works but not the pinch to zoom. If I use two fingures, it moves offscreen without zooming. Any ideas? Thanks.


    Code (CSharp):
    1. using UnityEngine;
    2. using UnityEngine.UI;// we need this namespace in order to access UI elements within our script
    3. using System.Collections;
    4.  
    5. public class MoveCamera : MonoBehaviour
    6.     // Credit to damien_oconnell from http://forum.unity3d.com/threads/39513-Click-drag-camera-movement
    7.     // for using the mouse displacement for calculating the amount of camera movement and panning code.
    8.    
    9. {
    10.     //
    11.     // VARIABLES
    12.     //
    13.    
    14.     public float turnSpeed = 4.0f;        // Speed of camera turning when mouse moves in along an axis
    15.     public float panSpeed = 4.0f;        // Speed of the camera when being panned
    16.     public float zoomSpeed = 4.0f;        // Speed of the camera going back and forth
    17.    
    18.     private Vector3 mouseOrigin;    // Position of cursor when mouse dragging starts
    19.     private bool isPanning;        // Is the camera being panned?
    20.     private bool isRotating;    // Is the camera being rotated?
    21.     private bool isZooming;        // Is the camera zooming?
    22.    
    23.     //
    24.     // UPDATE
    25.     //
    26.    
    27.     void Update ()
    28.     {
    29.         // Get the left mouse button
    30.         if(Input.GetMouseButtonDown(0))
    31.         {
    32.             // Get mouse origin
    33.             mouseOrigin = Input.mousePosition;
    34.             isRotating = true;
    35.         }
    36.        
    37.         // Get the right mouse button
    38.         if(Input.GetMouseButtonDown(1))
    39.         {
    40.             // Get mouse origin
    41.             mouseOrigin = Input.mousePosition;
    42.             isPanning = true;
    43.         }
    44.        
    45.         // Get the middle mouse button
    46.         if(Input.GetMouseButtonDown(2))
    47.         {
    48.             // Get mouse origin
    49.             mouseOrigin = Input.mousePosition;
    50.             isZooming = true;
    51.         }
    52.        
    53.         // Disable movements on button release
    54.         if (!Input.GetMouseButton(0)) isRotating=false;
    55.         if (!Input.GetMouseButton(1)) isPanning=false;
    56.         if (!Input.GetMouseButton(2)) isZooming=false;
    57.        
    58.         // Rotate camera along X and Y axis
    59.         if (isRotating)
    60.         {
    61.             Vector3 pos = Camera.main.ScreenToViewportPoint(Input.mousePosition - mouseOrigin);
    62.            
    63.             transform.RotateAround(transform.position, transform.right, pos.y * turnSpeed);
    64.             transform.RotateAround(transform.position, Vector3.up, -pos.x * turnSpeed);
    65.         }
    66.        
    67.         // Move the camera on it's XY plane
    68.         if (isPanning)
    69.         {
    70.             Vector3 pos = Camera.main.ScreenToViewportPoint(Input.mousePosition - mouseOrigin);
    71.            
    72.             Vector3 move = new Vector3(pos.x * panSpeed, pos.y * panSpeed, 0);
    73.             transform.Translate(move, Space.Self);
    74.         }
    75.        
    76.         // Move the camera linearly along Z axis
    77.         if (isZooming)
    78.         {
    79.             Vector3 pos = Camera.main.ScreenToViewportPoint(Input.mousePosition - mouseOrigin);
    80.            
    81.             Vector3 move = pos.y * zoomSpeed * transform.forward;
    82.             transform.Translate(move, Space.World);
    83.         }
    84.     }
    85. }
     
    dubbist likes this.
  2. StarManta

    StarManta

    Joined:
    Oct 23, 2006
    Posts:
    8,744
    Input.GetMouseButton is designed for.... a mouse. As it happens, Unity does emulate a mouse when on a touch screen, but only one button (0) and only one finger at a time.

    If you ever need more control than that (and with pinch to zoom, you do), then you'll need to use Input.touches. This will give you an array of touches (one for each finger on the screen), which each contain their own position and state data. To handle pinch to zoom, you'll need to store a number indicating the distance between your fingers on the previous frame. You'll check to see if Input.touchCount == 2, and if it is, you:

    1. Calculate the pixel distance between the fingers;
    2. If it's the first consecutive frame where .touchCount == 2, move the camera by some multiplier times newdistance / previousDistance
    3. Save newDistance into previousDistance for use in the next frame
     
  3. larku

    larku

    Joined:
    Mar 14, 2013
    Posts:
    1,422
    albert3094 likes this.
  4. StarManta

    StarManta

    Joined:
    Oct 23, 2006
    Posts:
    8,744
    That's a pretty handy script.
     
  5. schwooba

    schwooba

    Joined:
    Nov 25, 2013
    Posts:
    33
    Thanks StarManta and larku!

    @StarManta - Not sure how to implement that code into the existing script. Not sure if you could lend a hand :/

    @larku - I tried your script but it only allowed for zooming...not both zooming and rotating. Thanks anyway.
     
  6. larku

    larku

    Joined:
    Mar 14, 2013
    Posts:
    1,422
    Umm, that script explicitly handles both rotating (turn) and zooming.

    But even if it didn't, I intended for you to use the concepts in that script to build your own solution - though as it already does both it could be dropped in and used.
     
  7. rashed991

    rashed991

    Joined:
    May 24, 2016
    Posts:
    1
    شكر الك على جهودك
     
  8. Little_Master

    Little_Master

    Joined:
    Sep 14, 2017
    Posts:
    3