1
0

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.

(2)-2 カメラ回転実験 Quaternion.AngleAxis

Last updated at Posted at 2020-03-26

1. transform.Rotate()の続き

以前の記事は、取得したけどQuaternion全然使っていない事に気づきました。
Quaternionって軸で回すくらいの知識しか持ち合わせていないので実験してみたいと思いました。

で、どういう事を実現したいのか?

①Quaternionを宣言・取得したい

→現在の角度を手に入れるには、
Quaternion myQuaternion = this.transform.rotation;
👆左側、、、変数名(こだわり)はmyQuaternionに。
👆右側、、、アタッチしているオブジェクトの回転情報(transform.rotation)を入れてよと。

②Quaternionを更新したい

・マウスを加味して更新したいです。

 右に動かすとっての自身最大の武器GetAxisを使わずに。。

・マウスを加味してってのは

if (Input.GetKey(KeyCode.W))
👆キーボードのWボタンを押したらで実装します。

・AngleAxis関数の登場

Quaternion rot = Quaternion.AngleAxis(2, Vector3.right);
更新するのにいる「更新要素」みたいなものと理解しました。
👆左側、、、変数名rotに。
👆右側、、、毎秒2度dで、x軸を軸にして回転させる
ここで疑問が出た。x軸を軸にってVector3(1,0,0)をかけるって事?
いやこれオイラー角やん。。。こーゆうとこ蓋して進む。。。

・そして最後に合成

this.transform.rotation = myQuaternion * rot;
👆左側、、、自分に
👆右側、、、自分に「更新要素」のrot変数をかけて代入してくれ。

、、、感動、、、

01.JPG
02.JPG

動かせました。ちょっとだけ。。

・そして最後に

Quaternion rot = Quaternion.AngleAxis(2, Vector3.right);
の末尾のrightを変えると違う方向に回転してくれます。
right、left、up、downは使う感じあるけど
他は入れてみると挙動に驚く仕上がりに。。。

Vector3.zero; → Vector3(0,0,0) 不動
Vector3.right; → Vector3(1,0,0) 上回転
Vector3.left; → Vector3(-1,0,0) 下回転
Vector3.up; → Vector3(0,1,0) 右回転
Vector3.down; → Vector3(0,-1,0) 左回転
Vector3.forward; → Vector3(0,0,1) 右回転、地球が
Vector3.back; → Vector3(0,0,-1) 左回転、地球が
Vector3.one; →  Vector3(1,1,1) んー、なんともいえない右回転、


    void Update()
    {
        if (Input.GetKey(KeyCode.W))
        {
//Quaternionで回転するとこ
            Quaternion myQuaternion = this.transform.rotation;
            Quaternion rot = Quaternion.AngleAxis(2, Vector3.zero);
            this.transform.rotation = myQuaternion * rot;

//オイラー角をDebug.Logで。 
            float quat_x = myQuaternion.eulerAngles.x;
            float quat_y = myQuaternion.eulerAngles.y;
            float quat_z = myQuaternion.eulerAngles.z;
            Debug.Log("X=" + quat_x);
            Debug.Log("Y=" + quat_y);
            Debug.Log("Z=" + quat_z);
        }
    }
}

参考にさせて頂いたサイトです;感動をありがとうございました。

[【Unity】Quaternionでオブジェクトを回転させる方法]
(https://xr-hub.com/archives/11515)
[【Unity】Vector3の使い方とオブジェクトの移動や回転を表現する方法]
(https://xr-hub.com/archives/12115)

1
0
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
1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?