Skip to content

Instantly share code, notes, and snippets.

@peroon
Created June 16, 2015 15:26
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save peroon/2e5c3eea47238cac7268 to your computer and use it in GitHub Desktop.
Save peroon/2e5c3eea47238cac7268 to your computer and use it in GitHub Desktop.
ピンチインカメラ(エディタではなく端末向け)
using UnityEngine;
using System.Collections;
public class PinchInCamera : MonoBehaviour {
public float minHeight = 1.0f;
public float maxHeight = 10.0f;
private float touchDistance = 0.0f;
private float scaleSpeed = 0.1f;
void Update () {
if (Input.touchCount >= 2)
{
Vector2 touch0, touch1;
touch0 = Input.GetTouch(0).position;
touch1 = Input.GetTouch(1).position;
// 触れ始めが基準
if(Input.GetTouch(1).phase == TouchPhase.Began){
touchDistance = scaleSpeed * Vector2.Distance(touch0, touch1);
}
// 動いたときにカメラの高さを変える
else if(Input.GetTouch(1).phase == TouchPhase.Moved){
float distance = scaleSpeed * Vector2.Distance(touch0, touch1);
float diff = distance - touchDistance;
this.transform.position -= Vector3.up * diff;
// cap
var pos = this.transform.position;
if(pos.y < minHeight){
this.transform.position = new Vector3(pos.x, minHeight, pos.z);
}else if (pos.y > maxHeight){
this.transform.position = new Vector3(pos.x, maxHeight, pos.z);
}
// update
touchDistance = distance;
}
}
if (Input.touchCount == 1) {
Debug.Log(Input.GetTouch(0).position);
if(Input.GetTouch(0).phase == TouchPhase.Moved){
var delta = Input.GetTouch(0).deltaPosition;
float moveScale = 0.1f;
this.transform.position -= new Vector3(delta.x, 0, delta.y) * moveScale;
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment