title: Swipe screen to rotate camera
body: <p>I am writing a 3D game for smartphone using Unity3D and have no idea on making camera rotation (First Person Perspective).</p>
  
  <p>Making virtual joystick is too hard, so I decided to make camera rotate on screen swipe.</p>
  
  <p>User swipe smartphone's screen and camera turns around.</p>
  
  <p>Rotating works, but always starts from the same position.</p>
  
  <p>Saving last position breaks all rotating (rotating is askew).</p>
  
  <p>My code:</p>
  
  <pre><code>using UnityEngine;
  using System.Collections;
  
  public class Look : MonoBehaviour {
  
  public float sensitivityX = 1F;
  public float sensitivityY = 1F;
  
  public float minimumX = -360F;
  public float maximumX = 360F;
  
  public float minimumY = -25F;
  public float maximumY = 25F;
  
  float rotationX = 0F;
  float rotationY = 0F;
  
  float oldRotationX = 0F;
  float oldRotationY = 0F;
  
  float lastX = 0F;
  float lastY = 0F;
  
  Quaternion originalRotation;
  
  void Update ()
  {
      if (Input.touches.Length &gt; 0)
      {
          if(Input.touches[0].phase == TouchPhase.Began)
          {
              lastX = Input.touches[0].position.x;
              lastY = Input.touches[0].position.y;
  
              rotationX = transform.localEulerAngles.x;
              rotationY = transform.localEulerAngles.y;
  
              oldRotationX = rotationX;
              oldRotationY = rotationY;
          }
  
          if(Input.touches[0].phase == TouchPhase.Moved)
          {
              rotationX = (oldRotationX + (Input.touches[0].position.x - lastX)) * sensitivityX;
              rotationY = (oldRotationY + (Input.touches[0].position.y - lastY)) * sensitivityY;
  
              rotationX = ClampAngle (rotationX, minimumX, maximumX);
              rotationY = ClampAngle (rotationY, minimumY, maximumY);
  
              Quaternion xQuaternion = Quaternion.AngleAxis (rotationX, Vector3.up);
              Quaternion yQuaternion = Quaternion.AngleAxis (rotationY, -Vector3.right);
  
              transform.localRotation = originalRotation * xQuaternion * yQuaternion;
          }
  
          //this should make rotating from last position, but it
          //makes rotating incorrect (askew)
          //without code below rotating works, but on always
          //starts from the same position
          if(Input.touches[0].phase == TouchPhase.Ended)
          {
              originalRotation = transform.localRotation;
          }
      }
  }
  
  void Start ()
  {
      if (rigidbody)
          rigidbody.freezeRotation = true;
  
      originalRotation = transform.localRotation;
  }
  
  public static float ClampAngle (float angle, float min, float max)
  {
      if (angle &lt; -360F)
          angle += 360F;
      if (angle &gt; 360F)
          angle -= 360F;
      return Mathf.Clamp (angle, min, max);
  }
  }
  </code></pre>
  
  <p>Please help me. Thanks.</p>