Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Move camera forward/backward based on rotation using touch?

Discussion in 'Scripting' started by Durins-Bane, May 14, 2017.

  1. Durins-Bane

    Durins-Bane

    Joined:
    Sep 21, 2012
    Posts:
    175
    I have a camera that moves based on touch direction. How do I change the direction the camera moves based on the way its facing?

    At the minute it just moves along the fixed X,Z how do I get it to move based on the camera?

    First time using touches like this for mobile so have no idea what I am doing :(

    Heres the code I have, in Tapcount == 1 is where the camera is moved based on the direction of the swipe.

    Code (CSharp):
    1. using UnityEngine;
    2.  
    3. public class CamControl : MonoBehaviour {
    4.     public float perspectiveZoomSpeed = 0.5f;        // The rate of change of the field of view in perspective mode.
    5.     public float orthoZoomSpeed = 0.5f;        // The rate of change of the orthographic size in orthographic mode.
    6.     Camera cam;
    7.     public bool zooming = false;
    8.  
    9.     Vector3 oldPos = new Vector3(0, 0, 0);
    10.     Vector3 panOrigin = new Vector3(0, 0, 0);
    11.  
    12.     bool bDragging = false;
    13.     public float panSpeed = 5;
    14.  
    15.     private void Awake() {
    16.         cam = transform.GetChild(0).gameObject.GetComponent<Camera>();
    17.     }
    18.  
    19.     void LateUpdate() {
    20.         if (Input.touchCount == 1) {
    21.                  if (!zooming) {
    22.  
    23.                 Touch touchZero = Input.GetTouch(0);
    24.                 TouchPhase phase0 = touchZero.phase;
    25.  
    26.                 switch (phase0) {
    27.                     case TouchPhase.Began:
    28.                         oldPos = transform.localPosition;
    29.                         panOrigin = cam.ScreenToViewportPoint(touchZero.position);
    30.                         break;
    31.  
    32.  
    33.                     case TouchPhase.Moved:
    34.                         Vector3 pos = cam.ScreenToViewportPoint(touchZero.position) - panOrigin;   //Get the difference between where the mouse clicked and where it moved
    35.                         Vector3 newPos;
    36.                         newPos.x = pos.x;
    37.                         newPos.y = pos.z;  //z in place of y
    38.                         newPos.z = pos.y;  //y in place of z
    39.                         Vector3 v = oldPos + -newPos * panSpeed;                                           //Move the position of the camera to simulate a drag, speed * 10 for screen to worldspace conversion
    40.                  
    41.                
    42.                         if (transform.localPosition.y < 0.5f) {
    43.                             transform.localPosition = new Vector3(transform.localPosition.x, 0.5f, transform.localPosition.z);
    44.                         }
    45.                         if (transform.localPosition.y > 3f) {
    46.                             transform.localPosition = new Vector3(transform.localPosition.x, 3, transform.localPosition.z);
    47.                         }
    48.                         if (transform.localPosition.x < 10f) {
    49.                             transform.localPosition = new Vector3(10, transform.localPosition.y, transform.localPosition.z);
    50.                         }
    51.                         if (transform.localPosition.x > 30f) {
    52.                             transform.localPosition = new Vector3(30, transform.localPosition.y, transform.localPosition.z);
    53.                         }
    54.  
    55.                     break;
    56.  
    57.                     case TouchPhase.Ended:
    58.                         bDragging = false;
    59.                         zooming = false;
    60.                         break;
    61.  
    62.                     case TouchPhase.Canceled:
    63.                         bDragging = false;
    64.                         zooming = false;
    65.                         break;
    66.                 }
    67.             }
    68.         } else if (Input.touchCount == 2) {
    69.  
    70.             Touch touchZero = Input.GetTouch(0);
    71.             Touch touchOne = Input.GetTouch(1);
    72.  
    73.             TouchPhase phase0 = touchZero.phase;
    74.             TouchPhase phase1 = touchOne.phase;
    75.  
    76.             switch (phase1) {
    77.                 case TouchPhase.Began:
    78.                     zooming = true;
    79.                     break;
    80.                 case TouchPhase.Ended:
    81.                     zooming = false;
    82.                     break;
    83.             }
    84.  
    85.             switch (phase0) {
    86.                 case TouchPhase.Ended:
    87.                     zooming = false;
    88.                     break;
    89.             }
    90.      
    91.             float pinchAmount = 0;
    92.             Quaternion desiredRotation = transform.rotation;
    93.             detectTouchMovement.Calculate();
    94.             if (Mathf.Abs(detectTouchMovement.turnAngleDelta) > 0) {
    95.                 // rotate
    96.                 Vector3 rotationDeg = Vector3.zero;
    97.                 rotationDeg.y = -detectTouchMovement.turnAngleDelta;
    98.                 // rotationDeg.x = -detectTouchMovement.turnAngleDelta;
    99.                 desiredRotation *= Quaternion.Euler(rotationDeg);      
    100.                 transform.rotation = desiredRotation;
    101.             }
    102.  
    103.             // Find the position in the previous frame of each touch.
    104.             Vector2 touchZeroPrevPos = touchZero.position - touchZero.deltaPosition;
    105.             Vector2 touchOnePrevPos = touchOne.position - touchOne.deltaPosition;
    106.  
    107.             // Find the magnitude of the vector (the distance) between the touches in each frame.
    108.             float prevTouchDeltaMag = (touchZeroPrevPos - touchOnePrevPos).magnitude;
    109.             float touchDeltaMag = (touchZero.position - touchOne.position).magnitude;
    110.  
    111.             // Find the difference in the distances between each frame.
    112.             float deltaMagnitudeDiff = prevTouchDeltaMag - touchDeltaMag;
    113.  
    114.             // If the camera is orthographic...
    115.             if (cam.orthographic) {
    116.                 // ... change the orthographic size based on the change in distance between the touches.
    117.                 cam.orthographicSize += deltaMagnitudeDiff * orthoZoomSpeed;
    118.  
    119.                 // Make sure the orthographic size never drops below zero.
    120.                 cam.orthographicSize = Mathf.Max(cam.orthographicSize, 0.1f);
    121.             } else {
    122.  
    123.                 // Otherwise change the field of view based on the change in distance between the touches.
    124.                 cam.fieldOfView += deltaMagnitudeDiff * perspectiveZoomSpeed;
    125.  
    126.                 // Clamp the field of view to make sure it's between 0 and 180.
    127.                 cam.fieldOfView = Mathf.Clamp(cam.fieldOfView, 20, 70);
    128.             }
    129.         }
    130.     }
    131. }
    https://giant.gfycat.com/HugePreciousFlyinglemur.gif
     
    Last edited: May 14, 2017