title: Rotating gameobject using touch in Unity 3d with C#
body: <p>I'm rotating a GameObject based on 2 finger touch. I have the rotate working fine, but I'm getting a strange issue where sometimes I will touch with two fingers to start the rotate, but as soon as I touch the screen the object will instantly rotate. This seems to be random as to when it does it, and the rotation also appears to be random. I'm thinking that the touch is triggering the rotate based on the last position, but my code should be resetting the start position.</p>
  
  <p>The touch code is in a script attached to the object to rotate, so it's all in one file.</p>
  
  <p>Here's the code.</p>
  
  <p>In the Update method I detect that it's touch supported and attempt to call "HandleTouch()"</p>
  
  <pre><code>void Update()
  {
      if (Input.touchSupported)
          HandleTouch();
      else
          HandleMouse();
  }
  </code></pre>
  
  <p>The HandleTouch method</p>
  
  <pre><code>private void HandleTouch()
  {
      if (Input.touchCount &lt; 2) return;
      switch (Input.touchCount)
      {
          case 2:
              Touch touch = Input.GetTouch(0);
              if (touch.phase == TouchPhase.Began)
              {
                  lastRotPosition = touch.position;
              }
              else if (touch.phase == TouchPhase.Moved)
              {
                  Vector3 offset = touch.position - lastRotPosition;
                  lastRotPosition = touch.position;
                  RotateCamera(offset.x * RotateSpeedTouch, offset.y * RotateSpeedTouch);
              }
              else if (touch.phase == TouchPhase.Ended)
              {
                  lastRotPosition = new Vector2();
              }
              break;
      }
  }
  </code></pre>
  
  <p>And this is the method that rotates the GameObject</p>
  
  <pre><code>void RotateCamera(float x, float y)
  {
      float rotX = x * rotateSpeed * Mathf.Deg2Rad;
      float rotY = y * rotateSpeed * Mathf.Deg2Rad;
      transform.Rotate(Vector3.up, rotX);
      transform.Rotate(Vector3.right, -rotY);
  }
  </code></pre>
  
  <p>Thank you for any help or insight you might offer.</p>