2
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

[Unity]スワイプ操作、マウス操作でオブジェクトをくるくるさせるやつの作り方

Posted at

ビューワーによくある、こういうやつを作ります。
A.gif

オブジェクトの構成

オブジェクトを固定し、入れ子にしたカメラの親を動かす形になっています。
キャプチャ.PNG

ソース

Vector3 StartFrick, NowRotation;

    void Start()
    {
        //角度の初期化
        NowRotation = new Vector3(-360, 0, 0);
    }

    void Update()
    {
        if (Input.GetKeyDown(KeyCode.Mouse0))
        {
            //現在の角度と画面にタッチしたVector3値の和を格納し、Update関数を終了
            StartFrick = Input.mousePosition + NowRotation;
            return;
        }

        if (Input.GetKey(KeyCode.Mouse0))
        {
            //マウスでクリックし続けてる間のみ、
            //現在のタッチしているVector3値とStartFrickの差分で角度を変更する
            NowRotation = StartFrick - Input.mousePosition;
            gameObject.transform.localEulerAngles = new Vector3(NowRotation.y * 0.5f, -NowRotation.x * 0.5f, 0);
        }

もっと良い書き方があったらコメントください。

2
2
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
2
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?