LoginSignup
11
10

More than 3 years have passed since last update.

2本指の回転ドラッグ操作で回転判定

Last updated at Posted at 2016-05-02

目的

Unityで2本指の回転ドラッグ操作で回転判定をしたい.
画面の回転,オブジェクトの回転などで利用することを想定.

方法

2点間からなる角度を算出をして,前回の結果と比較して判定する.

コード

Input.GetTouch()でタッチした位置を取得



enum RotateDirection {
  NONE,
  LEFT,
  RIGHT
}

int _preDegree;  //前回の角度を保持

void MultiTouch () {
  Touch touch1 = Input.GetTouch(0);
  Touch touch2 = Input.GetTouch(1);
  Vector2 pos1 = touch1.position;
  Vector2 pos2 = touch2.position;

  RotateDirection rotateDirection = GetRotateDirection (pos1, pos2);

  if (rotateDirection == RotateDirection.LEFT) {
    Debug.Log ("左回り");
  } else if (rotateDirection == RotateDirection.RIGHT) {
    Debug.Log ("右回り");
  } else {
    // 回転なし
  }
}

RotateDirection GetRotateDirection (Vector2 pos1, Vector pos2) {
  // x座標の位置でpos1とpos2を入れ替える
  if (pos1.x > pos2.x) {
    Vector2 tmp = pos1;
    pos1 = pos2;
    pos2 = tmp;
  }

  float dx = pos2.x - pos1.x;
  float dy = pos2.y - pos1.y;
  float rad = Mathf.Atan2 (dy, dx);
  int degree = Mathf.RoundToInt (rad * Mathf.Rad2Deg);
  RotateDirection rotateDirection = RotateDirection.NONE;
  if (degree - preDegree > 0) {
    rotateDirection = RotateDirection.LEFT;
  } 
  if (degree - preDegree < 0) {
    rotateDirection = RotateDirection.RIGHT;
  }
  preDegree = degree;
  return rotateDirection;
}


参考

【Unity2D】2点間の角度を求める

11
10
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
11
10