iLMS知識社群歷程檔Login
Position: 黃國哲 > Unity
1101016 分段變數型地圖縮放
by 黃國哲 2021-10-16 12:38:02, Reply(0), Views(303)
using UnityEngine;
using System.Collections;
using System.IO;

public class 地圖縮放移動腳本 : MonoBehaviour
{
//650 100
public GameObject movetarget;
public GameObject zoomtarget;
public float movespeed1; //設定遠距離移動速度1
public float movespeed2; //設定中距離移動速度2
public float movespeed3; //設定近距離移動速度3
public float movespeed4; //設定超近距離移動速度4

public float zoomspeed1; //設定遠距離縮放速度1
public float zoomspeed2; //設定中距離縮放速度2
public float zoomspeed3; //設定近距離縮放速度3
public float zoomspeed4; //設定超近距離縮放速度4

public float cameraposition; //宣告成功開變數用來監看Camera的Z軸位置
private Touch oldTouch1;  //上次觸控點1(手指1)
private Touch oldTouch2;  //上次觸控點2(手指2)

void Start()
{ }

void Update()
{
cameraposition = zoomtarget.GetComponent<Transform>().position.y; //隨時更新Camera的Y軸位置

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

//單點觸控, 水平上下移動
if (Input.touchCount == 1 && Input.GetTouch(0).phase == TouchPhase.Moved)
{
var deltaposition = Input.GetTouch(0).deltaPosition;


if (cameraposition >= 2500)
{ movetarget.transform.Translate(deltaposition.x * movespeed1, 0f, deltaposition.y * movespeed1); }

else if (cameraposition < 2500)
{ movetarget.transform.Translate(deltaposition.x * movespeed2, 0f, deltaposition.y * movespeed2); }

else if (cameraposition <= 1000)
{ movetarget.transform.Translate(deltaposition.x * movespeed3, 0f, deltaposition.y * movespeed3); }

else if (cameraposition < 600)
{ movetarget.transform.Translate(deltaposition.x * movespeed4, 0f, deltaposition.y * movespeed4); }

}

//單點觸摸, 水平上下旋轉  
//if (1 == Input.touchCount)
//{
// Touch touch = Input.GetTouch(0);
// Vector2 deltaPos = touch.deltaPosition;
// movetarget.transform.Rotate(Vector3.down * deltaPos.x, Space.World);
// movetarget.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可調整)

if (cameraposition >= 2500)
{
float scaleFactor = offset * zoomspeed1;
{ zoomtarget.transform.Translate(Vector3.forward * scaleFactor, Space.Self); }
}

else if (cameraposition < 2500)
{
float scaleFactor = offset * zoomspeed2;
{ zoomtarget.transform.Translate(Vector3.forward * scaleFactor, Space.Self); }
}

else if (cameraposition <= 1000)
{
float scaleFactor = offset * zoomspeed3;
{ zoomtarget.transform.Translate(Vector3.forward * scaleFactor, Space.Self); }
}

else if (cameraposition < 600)
{
float scaleFactor = offset * zoomspeed4;
{ zoomtarget.transform.Translate(Vector3.forward * scaleFactor, Space.Self); }
}
//{ zoomtarget.transform.Translate(Vector3.forward * scaleFactor, Space.Self); }

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

}
Reply