I am trying to use zoom with two fingers but unfortunately whenever I place two fingers together, the starting value of camera.fieldOfView changes drastically. As in it is not able to read the current value. I feel it goes to some other value. It should start from the current value. What am I doing wrong here?
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.EventSystems;
using UnityEngine.InputSystem.EnhancedTouch;
using Touch = UnityEngine.InputSystem.EnhancedTouch.Touch;
public Camera camera;
float touchDist = 0;
float lastDist = 0;
public float maxAngle = 60;
public float minAngle = 30;
private void Start()
{
EnhancedTouchSupport.Enable();
}
public void Update()
{
if (Touch.activeFingers.Count == 2)
{
Touch activeTouch = Touch.activeFingers[0].currentTouch;
Touch touch1 = Touch.activeFingers[0].currentTouch;
Touch touch2 = Touch.activeFingers[1].currentTouch;
if (touch1.phase == UnityEngine.InputSystem.TouchPhase.Began && touch2.phase == UnityEngine.InputSystem.TouchPhase.Began)
{
lastDist = Vector2.Distance(touch1.screenPosition, touch2.screenPosition);
}
if (touch1.phase == UnityEngine.InputSystem.TouchPhase.Moved && touch2.phase == UnityEngine.InputSystem.TouchPhase.Moved)
{
float newDist = Vector2.Distance(touch1.screenPosition, touch2.screenPosition);
touchDist = lastDist - newDist;
lastDist = newDist;
// Your Code Here
camera.fieldOfView += touchDist * 0.05f;
if (camera.fieldOfView > maxAngle)
{
camera.fieldOfView = maxAngle;
}
if (camera.fieldOfView < minAngle)
{
camera.fieldOfView = minAngle;
}
}
}
}
}