Camera Touch Zoom

hello!
I have written a script witch should zoom the camera in and out by swiping the fingers together on touchscreen. The problem is, that the camera only zooms, then one finger is moving, and the other is fixed. I want only to zoom then both fingers are moving.
Here my Script:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class CameraZoom : MonoBehaviour {
    float previosDistance;
    float zoomSpeed = 1.0f;



    void Update () {
        if (Input.touchCount == 2 &&
         (Input.GetTouch (0).phase == TouchPhase.Moved || Input.GetTouch (1).phase == TouchPhase.Began)) {
            // recalebrate distance between fingers
            previosDistance = Vector2.Distance(Input.GetTouch(0).position, Input.GetTouch(1).position);

        } else if (Input.touchCount == 2 &&
            (Input.GetTouch (0).phase == TouchPhase.Moved || Input.GetTouch (1).phase == TouchPhase.Moved)) {
            float distance;
            Vector2 touch1 = Input.GetTouch (0).position;
            Vector2 touch2 = Input.GetTouch (1).position;
            distance = Vector2.Distance (touch1, touch2);

            //Set Camera
            float amound = (previosDistance-distance)*zoomSpeed*Time.deltaTime;
            // for perpective    
        //    transform.Translate (0, 0, amound);
            // for Otrhographic
            Camera.main.orthographicSize += amound;

            previosDistance = distance;



        }
    
    
    
    
    
    }
}

Hope you can help me!

Your first and second if statement check if touch count is 2 and touch[0].phase is “Moved”. Because your second statement is an “else if”, you will always enter your first “if” statement when your touch[0] has moved, so it makes sense that it only works when that finger is still.

And how can i fix that?

for the easiest implementation, you do not need to care about the touch phases (you cannot rely on them as well).

Just check, if there are exactly two fingers on the screen and then get the screenpositions of them and calculate the pivot point and distance, any youre done. Its nothing more then 4 lines of code for the whole zoom thing.

You mean i only have to remove the touch phase?

more or less. You still need the start-position, to calculate the precise pivot-point and scale-factor.

Or you could do something like this (untestet, written in notepad, but should work):

Dictionary<int, Vector2> activePointers = new Dictionary<int, Vector2>();
Vector2 startPivotPoint = new Vector2();
float startDistance = 0f;
if (Input.touchCount == 2) {
    foreach (Touch t in Input.touches) {
        if (t.phase == TouchPhase.Began) activePointers.Add(t.fingerId, t.position);
        if (t.phase == TouchPhase.Moved) activePointers[t.fingerId] = t.position;
        if (t.phase == TouchPhase.Ended || t.phase == TouchPhase.Canceled) activePointers.remove(t.fingerId);
    }
    if (activePointers.Count == 2) {
        if (startDistance == 0f && startPivotPoint == new Vector2()) {
            //new pinch has begun
            Vector2 pinchVec = activePointers[0] - activePointers[1];
            startDistance = pinchVec.magnitude;
            startPivotPoint = activePointers[1] + (pinchVec * 0.5f);
        } else {
            Vector2 pinchVec = activePointers[0] - activePointers[1];
            float scaleFactor = pinchVec.magnitude / startDistance;
        }
    } else {
        //reset start values
        startDistance = 0f;
        startPivotPoint = new Vector2();
    }
}

But i am using orthographic Camera

and? I dont see the problem. My approach is completely independent from what youre trying to accomplish. You can use that code-snippet for every kind of pinch. What you do with “scaleFactor” is up to you.

If you want pixel-perfect pinch, than you just need to map the touch position on to whatever is below the finger-tips.

Thank you but there is the following error while executing

eyNotFoundException: The given key was not present in the dictionary.
System.Collections.Generic.Dictionary`2[System.Int32,UnityEngine.Vector2].get_Item (Int32 key) (at /Users/builduser/buildslave/mono/build/mcs/class/corlib/System.Collections.Generic/Dictionary.cs:150)
CameraZoom.Update () (at Assets/Scripts/CameraZoom.cs:25)

make a debug.log for the fingerId, maybe youre getting something different on your system as I on mine. For me the fingerId is always 0 for the first touchdown and 1 for the next and so on.

Me to i am using an other solution now. But thank you very much

and which one? Others might find this thread in the future and want to know your solution too

I am sorry but the script i am using now, works not correctly. I am working on it and will post it here then its done.