2
\$\begingroup\$

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.

                }
            }
        }
    }
\$\endgroup\$
2
  • \$\begingroup\$ Is that script attached to your main camera? Check out savalishunitytutorials.com as there is a clash of clans style camera movement script there. \$\endgroup\$
    – Savlon
    Commented Jun 11, 2015 at 10:51
  • \$\begingroup\$ Add a Debug.Log() to make sure its reaching your inner loop and remove Time.deltaTime from your final calculation. Basically your saying if the User slid his fingers 30 pixels this frame then multiply that by 0.001 or whatever small amount of time has passed since your last frame which results in camera movement so small you don't see it. Instead replace it with a Speed variable you can adjust in the editor. \$\endgroup\$ Commented Jun 11, 2015 at 12:52

1 Answer 1

1
\$\begingroup\$

From the Unity documentation:

Moved   A finger moved on the screen.
Stationary  A finger is touching the screen but hasn't moved.

You are checking to see that the fingers are both station and have moved. It is either one of the other. Your code inside the if statements will never run.

I see what you are trying to do, but it does not wait after one if statement for the next to be true. After your 2nd/3rd if statement is false, it exits all of the if statements. When it comes back around, it will start at the first if statement. It will not pick up where you left off.

You need to do something along these lines:

if (Input.touchCount == 2 && Input.GetTouch(0).phase == TouchPhase.Moved) {
    //do stuff
}
\$\endgroup\$

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .