I'm looking to achieve a two finger smooth pan system in my project. However, the code below does not work, any interaction with the screen doesn't change to position of the camera.
I am unsure where did I go wrong as I'm quite new to Unity.
Any help will be greatly appreciated.
void Update () {
//Detect If there are a) two fingers, b) touching the screen.
if (Input.touchCount == 2 && Input.GetTouch(0).phase == TouchPhase.Began)
{
if (Input.GetTouch(0).phase == TouchPhase.Began && Input.GetTouch(1).phase == TouchPhase.Began)
{
if (Input.GetTouch(0).phase == TouchPhase.Stationary && Input.GetTouch(1).phase == TouchPhase.Began)
{
if (Input.GetTouch(0).phase == TouchPhase.Moved && Input.GetTouch(1).phase == TouchPhase.Began)
{
Vector2 defaultCamera = transform.position;
Touch touchZero = Input.GetTouch(0); //Get locality of first touch.
Touch touchOne = Input.GetTouch(1); // Get locality of second touch.
Vector2 originalMid = Vector2.Lerp(touchZero.position, touchOne.position, 0.5f);
// Find the position in the previous frame of each touch.
Vector2 touchZeroPrevPos = touchZero.position - touchZero.deltaPosition;
Vector2 touchOnePrevPos = touchOne.position - touchOne.deltaPosition;
Vector2 touchMid = Vector2.Lerp(touchZeroPrevPos, touchOnePrevPos, 0.5f); //Get locality of midpoint between touches.
// Find the magnitude of the vector (the distance) between the touches in each frame.
Vector2 finalTouch = touchMid - originalMid;
transform.position = defaultCamera + finalTouch * Time.deltaTime; //transform camera using the new vector.
}
}
}
}