LoginSignup
23
21

More than 5 years have passed since last update.

Unity 2D 画面タッチでカメラ移動する

Last updated at Posted at 2014-11-09

Unityでタッチ判定を使って画面を横に移動する機能を作ってみた。
*3Dではなく2Dでの画面移動です

OS: Mac OSX 10.9.5
Unity ver: 4.5.0f6
言語: C#

ツールやアセットを一切使わずに作成しました。(何らかのツールで簡単にできるよ!というのがありましたらご教示願います)

public class GeneralFuncController : MonoBehaviour {

    private bool scrollStartFlg = false; // スクロールが始まったかのフラグ
    private Vector2 scrollStartPos  =   new Vector2(); // スクロールの起点となるタッチポジション
    private static float SCROLL_END_LEFT = -15f; // 左側への移動制限(これ以上左には進まない)
    private static float SCROLL_END_RIGHT = 15f; // 右側への移動制限(これ以上右には進まない)
    private static float SCROLL_DISTANCE_CORRECTION = 0.8f; // スクロール距離の調整

    private Vector2 touchPosition   =   new Vector2(); // タッチポジション初期化
    private Collider2D collide2dObj =   null; // タッチ位置にあるオブジェクトの初期化

    // Use this for initialization
    void Start () {

    }

    // Update is called once per frame
    void Update () {
        if (Input.GetMouseButton(0)) {

            touchPosition   = Camera.main.ScreenToWorldPoint(Input.mousePosition);
            collide2dObj = Physics2D.OverlapPoint(touchPosition);

            if (scrollStartFlg == false && collide2dObj) {
                // タッチ位置にオブジェクトがあったらそのオブジェクトを取得する
          // スクロール移動とオブジェクトタッチの処理を区別するために記載しました
                GameObject obj = collide2dObj.transform.gameObject;
                Debug.Log(obj.name);
            }else{
          // タッチした場所に何もない場合、スクロールフラグをtrueに
                scrollStartFlg = true;
                if(scrollStartPos.x == 0.0f){
               // スクロール開始位置を取得
                    scrollStartPos   = Camera.main.ScreenToWorldPoint(Input.mousePosition);
                }else{
                    Vector2 touchMovePos    =   touchPosition;
                    if(scrollStartPos.x != touchMovePos.x){
                        // 直前のタッチ位置との差を取得する
                        float diffPos   =   SCROLL_DISTANCE_CORRECTION * (touchMovePos.x - scrollStartPos.x);

                        Vector2 pos = this.transform.position;
                        pos.x -= diffPos;
                        // スクロールが制限を超過する場合、処理を止める
                        if(pos.x > SCROLL_END_RIGHT || pos.x < SCROLL_END_LEFT){
                            return;
                        }
                        this.transform.position = pos;
                        scrollStartPos = touchMovePos;
                    }
                }
            }
        }else{
            // タッチを離したらフラグを落とし、スクロール開始位置も初期化する 
            scrollStartFlg  =   false;
            scrollStartPos  =   new Vector2();
        }
    }
}

上記スクリプトをシーンのCameraにひもづけるだけで使用可能になります。

23
21
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
23
21