Skip to content

Instantly share code, notes, and snippets.

@vend9520
Created October 15, 2016 18:38
Show Gist options
  • Select an option

  • Save vend9520/b7acfd87c85169e07885cc97be8c3935 to your computer and use it in GitHub Desktop.

Select an option

Save vend9520/b7acfd87c85169e07885cc97be8c3935 to your computer and use it in GitHub Desktop.
ピンチイン/ピンチアウトでモデルを拡大/縮小する処理
using UnityEngine;
using UnityEngine.UI;
using System.Collections;
public class Pinch : MonoBehaviour {
public float pinchSpeed = 0.05f; //ピンチ時移動速度
private GameObject camera; //メインカメラ
private Vector2[] beforePoint; //1フレーム前のポイント(各指)
private Vector2[] nowPoint; //現フレームのポイント(各指)
void Start() {
camera = this.transform.Find("MainCamera").gameObject;
beforePoint = new Vector2[2];
nowPoint = new Vector2[2];
}
void Update() {
//2本目の指が押下された際
if (Input.GetTouch(1).phase == TouchPhase.Began) {
beforePoint[0] = Input.GetTouch(0).position;
beforePoint[1] = Input.GetTouch(1).position;
}
//2本目の指が移動した場合
if (Input.GetTouch(1).phase == TouchPhase.Moved) {
nowPoint[0] = Input.GetTouch(0).position;
nowPoint[1] = Input.GetTouch(1).position;
//各指間の距離を取得し、前フレームとの差分の分、カメラを前後に移動させる
float nowPointDistance = Vector2.Distance(nowPoint[0], nowPoint[1]);
float beforePointDistance = Vector2.Distance(beforePoint[0], beforePoint[1]);
float distance = nowPointDistance - beforePointDistance;
distance *= pinchSpeed * Time.deltaTime;
//現在の角度に応じて移動方向を算出
Vector3 direction = Quaternion.Euler(0, camera.transform.localEulerAngles.y, 0) * new Vector3(0, 0, distance);
direction = transform.TransformDirection(direction);
camera.transform.position = new Vector3(camera.transform.position.x + direction.x, camera.transform.position.y + direction.y, camera.transform.position.z + direction.z);
//現フレームのポイントを格納
beforePoint[0] = nowPoint[0];
beforePoint[1] = nowPoint[1];
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment