1100317 兩指移動
by 黃國哲 2021-03-17 18:47:55, 回應(0), 人氣(587)
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using UnityEngine.EventSystems;
public class moveevent : MonoBehaviour
{
private Touch oldTouch1; //上次觸控點1(手指1)
private Touch oldTouch2; //上次觸控點2(手指2)
public GameObject player;
public GameObject player1;
void Start()
{
//Init();
}
public void Move(BaseEventData data)
{
//沒有觸控
if (Input.touchCount <= 0)
{
return;
}
//多點觸控, 放大縮小
Touch newTouch1 = Input.GetTouch(0);
Touch newTouch2 = Input.GetTouch(1);
//第2點剛開始接觸螢幕, 只記錄,不做處理
if (newTouch2.phase == TouchPhase.Began)
{
oldTouch2 = newTouch2;
oldTouch1 = newTouch1;
return;
}
//計算老的兩點距離和新的兩點間距離,變大要放大模型,變小要縮放模型
float oldDistance = Vector2.Distance(oldTouch1.position, oldTouch2.position);
float newDistance = Vector2.Distance(newTouch1.position, newTouch2.position);
//兩個距離之差,為正表示放大手勢, 為負表示縮小手勢
float offset = newDistance - oldDistance;
//移動因子,一個畫素按 0.01倍來算(100可調整)
float scaleFactor = offset / 300f;
{ player.transform.Translate(Vector3.forward * scaleFactor, Space.Self); }
//記住最新的觸控點,下次使用
oldTouch1 = newTouch1;
oldTouch2 = newTouch2;
}
}
回應