Search Unity

Mobile touch to Orbit pan and zoom Camera, without fix target in one script

Discussion in 'Scripting' started by Stromerz, Mar 19, 2018.

  1. Stromerz

    Stromerz

    Joined:
    Jan 25, 2018
    Posts:
    13
    Hello guys,
    Um very new in UNITY, last couple of days I was trying to make a camera in unity which will behave just like sketchfab camera.

    I found some example which works with mouse movement perfectly. But I need to make it works in Mobile as well, Since um very new in unity I couldn't figure out how to rewrite the code to make it works with touch, can anyone help me with this, Thanks in Advance

    Code (CSharp):
    1. Using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. [AddComponentMenu("Camera-Control/3dsMax Camera Style")]
    5. public class maxCamera : MonoBehaviour
    6. {
    7.      public Transform target;
    8.      public Vector3 targetOffset;
    9.      public float distance = 5.0f;
    10.      public float maxDistance = 20;
    11.      public float minDistance = .6f;
    12.      public float xSpeed = 200.0f;
    13.      public float ySpeed = 200.0f;
    14.      public int yMinLimit = -80;
    15.      public int yMaxLimit = 80;
    16.      public int zoomRate = 40;
    17.      public float panSpeed = 0.3f;
    18.      public float zoomDampening = 5.0f;
    19.      private float xDeg = 0.0f;
    20.      private float yDeg = 0.0f;
    21.      private float currentDistance;
    22.      private float desiredDistance;
    23.      private Quaternion currentRotation;
    24.      private Quaternion desiredRotation;
    25.      private Quaternion rotation;
    26.      private Vector3 position;
    27.      void Start() { Init(); }
    28.      void OnEnable() { Init(); }
    29.      public void Init()
    30.      {
    31.          //If there is no target, create a temporary target at 'distance' from the cameras current viewpoint
    32.          if (!target)
    33.          {
    34.              GameObject go = new GameObject("Cam Target");
    35.              go.transform.position = transform.position + (transform.forward * distance);
    36.              target = go.transform;
    37.          }
    38.          distance = Vector3.Distance(transform.position, target.position);
    39.          currentDistance = distance;
    40.          desiredDistance = distance;
    41.          //be sure to grab the current rotations as starting points.
    42.          position = transform.position;
    43.          rotation = transform.rotation;
    44.          currentRotation = transform.rotation;
    45.          desiredRotation = transform.rotation;
    46.          xDeg = Vector3.Angle(Vector3.right, transform.right);
    47.          yDeg = Vector3.Angle(Vector3.up, transform.up);
    48.      }
    49.      /*
    50.       * Camera logic on LateUpdate to only update after all character movement logic has been handled.
    51.       */
    52.      void LateUpdate()
    53.      {
    54.          // If Control and Alt and Middle button? ZOOM!
    55.          if (Input.GetMouseButton(2))
    56.          {
    57.              desiredDistance -= Input.GetAxis("Mouse Y") * Time.deltaTime * zoomRate * 0.125f * Mathf.Abs(desiredDistance);
    58.          }
    59.          // If middle mouse and left alt are selected? ORBIT
    60.          if (Input.GetMouseButton(0))
    61.          {
    62.              xDeg += Input.GetAxis("Mouse X") * xSpeed * 0.02f;
    63.              yDeg -= Input.GetAxis("Mouse Y") * ySpeed * 0.02f;
    64.              ////////OrbitAngle
    65.              //Clamp the vertical axis for the orbit
    66.              yDeg = ClampAngle(yDeg, yMinLimit, yMaxLimit);
    67.              // set camera rotation
    68.              desiredRotation = Quaternion.Euler(yDeg, xDeg, 0);
    69.              currentRotation = transform.rotation;
    70.              rotation = Quaternion.Lerp(currentRotation, desiredRotation, Time.deltaTime * zoomDampening);
    71.              transform.rotation = rotation;
    72.          }
    73.          // otherwise if middle mouse is selected, we pan by way of transforming the target in screenspace
    74.          else if (Input.GetMouseButton(2))
    75.          {
    76.              //grab the rotation of the camera so we can move in a psuedo local XY space
    77.              target.rotation = transform.rotation;
    78.              target.Translate(Vector3.right * -Input.GetAxis("Mouse X") * panSpeed);
    79.              target.Translate(transform.up * -Input.GetAxis("Mouse Y") * panSpeed, Space.World);
    80.          }
    81.          ////////Orbit Position
    82.          // affect the desired Zoom distance if we roll the scrollwheel
    83.          desiredDistance -= Input.GetAxis("Mouse ScrollWheel") * Time.deltaTime * zoomRate * Mathf.Abs(desiredDistance);
    84.          //clamp the zoom min/max
    85.          desiredDistance = Mathf.Clamp(desiredDistance, minDistance, maxDistance);
    86.          // For smoothing of the zoom, lerp distance
    87.          currentDistance = Mathf.Lerp(currentDistance, desiredDistance, Time.deltaTime * zoomDampening);
    88.          // calculate position based on the new currentDistance
    89.          position = target.position - (rotation * Vector3.forward * currentDistance + targetOffset);
    90.          transform.position = position;
    91.      }
    92.      private static float ClampAngle(float angle, float min, float max)
    93.      {
    94.          if (angle < -360)
    95.              angle += 360;
    96.          if (angle > 360)
    97.              angle -= 360;
    98.          return Mathf.Clamp(angle, min, max);
    99.      }
    100. }
     
  2. GroZZleR

    GroZZleR

    Joined:
    Feb 1, 2015
    Posts:
    2,174
  3. Stromerz

    Stromerz

    Joined:
    Jan 25, 2018
    Posts:
    13
    thanks for replay Grozzler, can you please help me rewrite this code
     
  4. GroZZleR

    GroZZleR

    Joined:
    Feb 1, 2015
    Posts:
    2,174
    What have you tried? What part of your changes aren't working?
     
  5. Stromerz

    Stromerz

    Joined:
    Jan 25, 2018
    Posts:
    13
    um using this code for camera pan and zoom, which works perfect for now


    // variables for camera pan
    public float speedPan;

    // variables for camera zoom in and out
    public float perspectiveZoomSpeed;
    public float orthoZoomSpeed;
    public Camera mainCamera;

    //variables for camera orbit
    public Vector3 FirstPoint;
    public Vector3 SecondPoint;
    public float xAngle; //angle for axes x for rotation
    public float yAngle;
    public float xAngleTemp; //temp variable for angle
    public float yAngleTemp;

    // Use this for initialization
    void Start ()
    {

    }

    // Update is called once per frame
    void Update ()
    {

    // This part is for camera pan only & for 2 fingers stationary gesture
    if (Input.touchCount>0 && Input.GetTouch(1).phase == TouchPhase.Moved)
    {

    Vector2 touchDeltaPosition = Input.GetTouch (0).deltaPosition;
    transform.Translate (-touchDeltaPosition.x * speedPan, -touchDeltaPosition.y * speedPan, 0);
    }


    //this part is for zoom in and out
    if (Input.touchCount == 2)
    {
    Touch touchZero = Input.GetTouch (0);
    Touch touchOne = Input.GetTouch (1);

    Vector2 touchZeroPreviousPosition = touchZero.position - touchZero.deltaPosition;
    Vector2 touchOnePreviousPosition = touchOne.position - touchOne.deltaPosition;

    float prevTouchDeltaMag = (touchZeroPreviousPosition - touchOnePreviousPosition).magnitude;
    float TouchDeltaMag = (touchZero.position - touchOne.position).magnitude;

    float deltaMagDiff = prevTouchDeltaMag - TouchDeltaMag;


    if (mainCamera.orthographic)
    {
    mainCamera.orthographicSize += deltaMagDiff * orthoZoomSpeed;
    mainCamera.orthographicSize = Mathf.Max (mainCamera.orthographicSize, .1f);
    } else
    {
    mainCamera.fieldOfView += deltaMagDiff * perspectiveZoomSpeed;
    mainCamera.fieldOfView = Mathf.Clamp (mainCamera.fieldOfView, .1f, 179.9f);
    }

    }

    }
    }

    [ICODE]
     
  6. Stromerz

    Stromerz

    Joined:
    Jan 25, 2018
    Posts:
    13
    um using this code for rotate, which is not what i want

    using System.Collections;
    using System.Collections.Generic;
    using UnityEngine;

    public class cameraRotate : MonoBehaviour {

    //Exp 2
    public Transform target;
    public float distance = 10.0f;
    public float xSpeed = 60.0f;
    public float ySpeed = 60.0f;
    public float yMinLimit = 10f;
    public float yMaxLimit = 60f;
    public float distanceMin = 5f;
    public float distanceMax = 10f;
    float x = 0.0f;
    float y = 0.0f;


    void Start()
    {
    Vector3 angles = transform.eulerAngles;
    float x = angles.y;
    float y = angles.x;
    // Make the rigid body not change rotation
    if (GetComponent<Rigidbody>())
    GetComponent<Rigidbody>().freezeRotation = true;
    }

    void LateUpdate()
    {
    if (target && Input.touchCount == 1 && Input.GetTouch(0).position.x > Screen.width / 2 && Input.GetTouch(0).phase == TouchPhase.Moved) //Just orbit touch without movement
    {
    Debug.Log("Orbiting! 1 touch");
    Orbit(Input.GetTouch(0));
    }
    else if (Input.touchCount == 2)
    {
    if (Input.GetTouch(0).position.x > Screen.width / 2 && Input.GetTouch(0).phase == TouchPhase.Moved)
    Orbit(Input.GetTouch(0)); //Movement was touched second
    else if (Input.GetTouch(1).position.x > Screen.width / 2 && Input.GetTouch(1).phase == TouchPhase.Moved)
    Orbit(Input.GetTouch(1)); //Movement was touched first
    }
    }

    void Orbit(Touch touch)
    {
    x += touch.deltaPosition.x * xSpeed * 0.02f /* * distance*/;
    y -= touch.deltaPosition.y * ySpeed * 0.02f /* * distance*/;
    y = ClampAngle(y, yMinLimit, yMaxLimit);
    Quaternion rotation = Quaternion.Euler(y, x, 0);
    //distance = Mathf.Clamp(distance - Input.GetAxis("Mouse ScrollWheel") * 5, distanceMin, distanceMax);
    RaycastHit hit;
    if (Physics.Linecast(target.position, transform.position, out hit))
    {
    // distance -= hit.distance;
    }
    Vector3 negDistance = new Vector3(0.0f, 0.0f, -distance);
    Vector3 position = rotation * negDistance + target.position;
    transform.rotation = rotation;
    transform.position = position;
    }

    public static float ClampAngle(float angle, float min, float max)
    {
    if (angle < -360F)
    angle += 360F;
    if (angle > 360F)
    angle -= 360F;
    return Mathf.Clamp(angle, min, max);

    }
    }
     
  7. Stromerz

    Stromerz

    Joined:
    Jan 25, 2018
    Posts:
    13
    I want my camera to orbit around an object but the user can change the target position by panning the camera with using 2 finger move,
    whatever the target is camera always rotate around that target position using 1 finger move
    & whatever the target is camera zoom with pinching
     
  8. Stromerz

    Stromerz

    Joined:
    Jan 25, 2018
    Posts:
    13
    right now the code um using for orbit around the object won't let me change the target while um panning with 2 fingers, also the distance is always remaining the same
     
  9. Liyou1085066875

    Liyou1085066875

    Joined:
    May 20, 2016
    Posts:
    1
    Code (CSharp):
    1.  
    2. using System.Collections;
    3. using System.Collections.Generic;
    4. using UnityEngine;
    5.  
    6. public class MobilemaxCamera : MonoBehaviour {
    7.  
    8.     public Transform target;
    9.     public Vector3 targetOffset;
    10.     public float distance = 5.0f;
    11.     public float maxDistance = 20;
    12.     public float minDistance = .6f;
    13.     public float xSpeed = 5.0f;
    14.     public float ySpeed = 5.0f;
    15.     public int yMinLimit = -80;
    16.     public int yMaxLimit = 80;
    17.     public float zoomRate = 10.0f;
    18.     public float panSpeed = 0.3f;
    19.     public float zoomDampening = 5.0f;
    20.  
    21.     private float xDeg = 0.0f;
    22.     private float yDeg = 0.0f;
    23.     private float currentDistance;
    24.     private float desiredDistance;
    25.     private Quaternion currentRotation;
    26.     private Quaternion desiredRotation;
    27.     private Quaternion rotation;
    28.     private Vector3 position;
    29.  
    30.     private Vector3 FirstPosition;
    31.     private Vector3 SecondPosition;
    32.     private Vector3 delta;
    33.     private Vector3 lastOffset;
    34.     private Vector3 lastOffsettemp;
    35.     //private Vector3 CameraPosition;
    36.     //private Vector3 Targetposition;
    37.     //private Vector3 MoveDistance;
    38.  
    39.  
    40.     void Start() { Init(); }
    41.     void OnEnable() { Init(); }
    42.  
    43.     public void Init()
    44.     {
    45.         //If there is no target, create a temporary target at 'distance' from the cameras current viewpoint
    46.         if (!target)
    47.         {
    48.             GameObject go = new GameObject("Cam Target");
    49.             go.transform.position = transform.position + (transform.forward * distance);
    50.             target = go.transform;
    51.         }
    52.  
    53.         distance = Vector3.Distance(transform.position, target.position);
    54.         currentDistance = distance;
    55.         desiredDistance = distance;
    56.  
    57.         //be sure to grab the current rotations as starting points.
    58.         position = transform.position;
    59.         rotation = transform.rotation;
    60.         currentRotation = transform.rotation;
    61.         desiredRotation = transform.rotation;
    62.  
    63.         xDeg = Vector3.Angle(Vector3.right, transform.right);
    64.         yDeg = Vector3.Angle(Vector3.up, transform.up);
    65.     }
    66.  
    67.     /*
    68.       * Camera logic on LateUpdate to only update after all character movement logic has been handled.
    69.       */
    70.     void LateUpdate()
    71.     {
    72.         // If Control and Alt and Middle button? ZOOM!
    73.         if (Input.touchCount==2)
    74.         {
    75.             Touch touchZero = Input.GetTouch (0);
    76.  
    77.             Touch touchOne = Input.GetTouch (1);
    78.  
    79.  
    80.  
    81.             Vector2 touchZeroPreviousPosition = touchZero.position - touchZero.deltaPosition;
    82.  
    83.             Vector2 touchOnePreviousPosition = touchOne.position - touchOne.deltaPosition;
    84.  
    85.  
    86.  
    87.             float prevTouchDeltaMag = (touchZeroPreviousPosition - touchOnePreviousPosition).magnitude;
    88.  
    89.             float TouchDeltaMag = (touchZero.position - touchOne.position).magnitude;
    90.  
    91.  
    92.  
    93.             float deltaMagDiff = prevTouchDeltaMag - TouchDeltaMag;
    94.  
    95.             desiredDistance += deltaMagDiff * Time.deltaTime * zoomRate * 0.0025f * Mathf.Abs(desiredDistance);
    96.         }
    97.         // If middle mouse and left alt are selected? ORBIT
    98.         if (Input.touchCount==1 && Input.GetTouch(0).phase == TouchPhase.Moved)
    99.         {
    100.             Vector2 touchposition = Input.GetTouch(0).deltaPosition;
    101.             xDeg += touchposition.x * xSpeed * 0.002f;
    102.             yDeg -= touchposition.y * ySpeed * 0.002f;
    103.             yDeg = ClampAngle(yDeg, yMinLimit, yMaxLimit);
    104.  
    105.         }
    106.         desiredRotation = Quaternion.Euler(yDeg, xDeg, 0);
    107.         currentRotation = transform.rotation;
    108.         rotation = Quaternion.Lerp(currentRotation, desiredRotation, Time.deltaTime * zoomDampening);
    109.         transform.rotation = rotation;
    110.  
    111.  
    112.         if (Input.GetMouseButtonDown (1))
    113.         {
    114.             FirstPosition = Input.mousePosition;
    115.             lastOffset = targetOffset;
    116.         }
    117.  
    118.         if (Input.GetMouseButton (1))
    119.         {
    120.             SecondPosition = Input.mousePosition;
    121.             delta = SecondPosition - FirstPosition;
    122.             targetOffset = lastOffset + transform.right * delta.x*0.003f + transform.up * delta.y*0.003f;
    123.  
    124.         }
    125.  
    126.         ////////Orbit Position
    127.  
    128.         // affect the desired Zoom distance if we roll the scrollwheel
    129.         desiredDistance = Mathf.Clamp(desiredDistance, minDistance, maxDistance);
    130.         currentDistance = Mathf.Lerp(currentDistance, desiredDistance, Time.deltaTime * zoomDampening);
    131.  
    132.         position = target.position - (rotation * Vector3.forward * currentDistance );
    133.  
    134.         position = position - targetOffset;
    135.  
    136.         transform.position = position;
    137.  
    138.  
    139.  
    140.  
    141.     }
    142.     private static float ClampAngle(float angle, float min, float max)
    143.     {
    144.         if (angle < -360)
    145.             angle += 360;
    146.         if (angle > 360)
    147.             angle -= 360;
    148.         return Mathf.Clamp(angle, min, max);
    149.     }
    150. }
    151.  
    hope this can help you!
     
    Last edited: Jun 14, 2018
  10. SamohtVII

    SamohtVII

    Joined:
    Jun 30, 2014
    Posts:
    141
    Dude that code is amazing. Thank you so much!
     
  11. shinip2

    shinip2

    Joined:
    May 1, 2019
    Posts:
    1
    Appreciate your help! Thx
     
  12. hosamre94

    hosamre94

    Joined:
    Jan 18, 2017
    Posts:
    2
     
  13. florinel2102

    florinel2102

    Joined:
    May 21, 2019
    Posts:
    1
    Liyou1085066875 you saved my night :) . Thanks a lot !