Converting pinch to zoom to new InputSystem

Viewed 213

I am trying to convert pinch to zoom from old input to new Input system using EnhancedTouch.Touch. Unfortunately I am clueless as to how to move forward from here? I am getting error as Operator '==' cannot be applied to operands of type 'TouchPhase' and 'TouchPhase'. How do I fix this?

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.EventSystems;
using UnityEngine.InputSystem.EnhancedTouch;
using Touch = UnityEngine.InputSystem.EnhancedTouch.Touch;

public class ZoomTouch : MonoBehaviour {
public Camera camera;
float touchDist = 0;
float lastDist = 0;

public void Update () {

if (Input.touchCount == 2) {
            Touch touch1 = Touch.activeFingers[0].currentTouch;
            Touch touch2 = Touch.activeFingers[0].currentTouch;

            if (touch1.phase == TouchPhase.Began && touch2.phase == TouchPhase.Began) {
                lastDist = Vector2.Distance (touch1.position, touch2.position);
            }

            if (touch1.phase == TouchPhase.Moved && touch2.phase == TouchPhase.Moved) {
                float newDist = Vector2.Distance (touch1.position, touch2.position);
                touchDist = lastDist - newDist;
                lastDist = newDist;

                camera.fieldOfView += touchDist * 0.1f;

            }
        }

    }
}
1 Answers

I hope this code works. However, I did not have a touch device to test it. This simple code works, takes the distance between the present and the past, and zooms in on their delta.

public float lastDistance;
public float distance;
void Update()
{
    if (Input.touchCount < 2) return;

    var touch1 = Input.GetTouch(0);
    var touch2 = Input.GetTouch(1);
    
    distance = Vector2.Distance(touch1.position, touch1.position);

    if (Mathf.Abs(lastDistance-distance) >= 10) lastDistance = distance; // for avoiding sharp zooms on first touch
    
    Camera.main.fieldOfView += distance-lastDistance * 0.1f;

    lastDistance = distance;
}
Related