[英]Converting pinch to zoom to new InputSystem
我正在嘗試使用 EnhancedTouch.Touch 將捏合縮放從舊輸入轉換為新輸入系統。 不幸的是,我對如何從這里前進一無所知? 我收到錯誤消息,因為Operator '==' cannot be applied to operands of type 'TouchPhase' and 'TouchPhase'
。 我該如何解決?
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;
}
}
}
}
我希望這段代碼有效。 但是,我沒有觸摸設備來測試它。 這個簡單的代碼可以工作,獲取現在和過去之間的距離,並放大它們的增量。
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;
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.