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 Touch Control limit

Discussion in 'Scripting' started by malak, Mar 24, 2015.

  1. malak

    malak

    Joined:
    Jan 23, 2014
    Posts:
    65
    Hello everybody ,

    Thanks by advance for your time and sorry for my bad english ;)

    I use a Camera Touch control script for move my camera in a scene but the problem there is no limit , so the camera can move so far from the interest and is the same for the zoom :(

    Can you please help me to edit the script ?
    thanks a lot for your help ;)

    This is the script :
    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class AdvancedCamera : MonoBehaviour {
    5.    
    6.     public float PanSpeed = 0.025F;
    7.     public float PinchSpeed = 0.05F;
    8.    
    9.     // Use this for initialization, or not. That works too.
    10.     void Start () {
    11.        
    12.     }
    13.    
    14.     // Update is called once per frame, of course.
    15.     void Update () {
    16.         // Check if we have one finger down, and if it's moved.
    17.         // You may modify this first portion to '== 1', to only allow pinching or panning at one time.
    18.         if (Input.touchCount > 0 && Input.GetTouch(0).phase == TouchPhase.Moved) {
    19.             Vector2 touchDeltaPosition = Input.GetTouch(0).deltaPosition;
    20.             // Translate along world cordinates. (Done this way so we can angle the camera freely.)
    21.             transform.position -= new Vector3(touchDeltaPosition.x * PanSpeed, 0, touchDeltaPosition.y * PanSpeed);
    22.         }
    23.        
    24.         // Check if we have two fingers down.
    25.         if ( Input.touchCount == 2 )
    26.         {
    27.             Touch touch1 = Input.GetTouch( 0 );
    28.             Touch touch2 = Input.GetTouch( 1 );
    29.            
    30.             // Find out how the touches have moved relative to eachother.
    31.             Vector2 curDist = touch1.position - touch2.position;
    32.             Vector2 prevDist = (touch1.position - touch1.deltaPosition) - (touch2.position - touch2.deltaPosition);
    33.            
    34.             float touchDelta = curDist.magnitude - prevDist.magnitude;
    35.            
    36.             // Translate along local coordinate space.
    37.             Camera.main.transform.Translate(0, 0, touchDelta * PinchSpeed);  
    38.         }
    39.     }
    40. }
    I want add a minimum and maximum for the zoom and the movement , can you help me ?

    Thanks .
     
  2. schwooba

    schwooba

    Joined:
    Nov 25, 2013
    Posts:
    33
    Did you ever figure out your issue? Do you mind sharing? Thanks.
     
  3. eisenpony

    eisenpony

    Joined:
    May 8, 2015
    Posts:
    971
    You should be able to just clamp the main camera's z components of its transform position at the end of the script.. Something like:

    Code (csharp):
    1. Camera.main.transform.Translate(0, 0, touchDelta * PinchSpeed);
    2. Camera.main.transform.position.z = Mathf.Clamp(Camera.main.transform.position.z, <min distance>, <max distance>);