iLMS知識社群歷程檔Login
Position: 黃國哲 > Unity
1100926 地圖縮放腳本 (採用)
by 黃國哲 2021-09-26 09:21:09, Reply(0), Views(382)
using UnityEngine;
using System.Collections;
using System.IO;

public class 地圖縮放移動腳本 : MonoBehaviour
{

public GameObject movetarget;
public GameObject zoomtarget;
private Touch oldTouch1;  //上次觸控點1(手指1)
private Touch oldTouch2;  //上次觸控點2(手指2)

void Start()
{ }

void Update()
{

//沒有觸控
if (Input.touchCount <= 0)
{
return;
}

//單點觸控, 水平上下移動
if (Input.touchCount == 1 && Input.GetTouch(0).phase == TouchPhase.Moved)
{
var deltaposition = Input.GetTouch(0).deltaPosition;
movetarget.transform.Translate(deltaposition.x * 0.1f, 0f, deltaposition.y * 0.1f);
}

//單點觸摸, 水平上下旋轉  
//if (1 == Input.touchCount)
//{
// Touch touch = Input.GetTouch(0);
// Vector2 deltaPos = touch.deltaPosition;
// target.transform.Rotate(Vector3.down * deltaPos.x, Space.World);
// target.transform.Rotate(Vector3.right * deltaPos.y, Space.World);
//}

//多點觸控, 放大縮小
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 / 5f;
{ zoomtarget.transform.Translate(Vector3.forward * scaleFactor, Space.Self); }

//記住最新的觸控點,下次使用
oldTouch1 = newTouch1;
oldTouch2 = newTouch2;
}

}

Reply