0
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)-1 カメラの回転実験 Rotate+GetAxis

Last updated at Posted at 2020-03-25

令和2年3月23日(月)です。transform.Rotate()をやってやりましょう

作戦?

 (1)現在の角度を取得
 (2)入力量により回転させる

(1)現在の角度を取得(確認)

インスペクターのオイラー角
X=0,Y=10,Z=0 に設定

①恐る恐る「Quaternion」で何が返ってくるのかを確認

    void Start()
    {
        Quaternion myQuaternion = this.transform.rotation;
        Debug.Log(myQuaternion);
    }

→Debug.Logで四元数が返って来たことを確認
(0.0, 0.1, 0.0, 1.0)

②QuaternionからEulerへ変換したらどうなるのかを確認

    void Start()
    {
        Quaternion myQuaternion = this.transform.rotation;
        Debug.Log(myQuaternion);
        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);
    }

→Debug.Logで「X=0,Y=10,Z=0」が返ってきた素晴らしい!

(2)入力量により回転させる

        //もし、Horizontalがマイナスなら
        if Input.GetAxis("Horizontal")<0)
        {
        //そのまま入れる的な
            transform.Rotate(0, Input.GetAxis("Horizontal"), 0);
        }
        //もし、Horizontalがプラスなら
        if Input.GetAxis("Horizontal")>0)
        {
        //そのまま入れる的な
            transform.Rotate(0, Input.GetAxis("Horizontal"), 0);
        }

(問題)
通常Input.GetAxis("Horizontal");でやるみたいだけど←矢印を上下左右で
使用しているのでぐちゃぐちゃな結果になるので、これは使えないのと
+
(疑問)
釈然としないのは、<0 符号要らなくない?
別シーンで試してみたらエラーがでる。

(疑問)から解決
エラー内容が、条件部分がfloatをboolに変換出来ないとの事で、
条件は真偽なのにGetAxisは数値なので、判定のために無理やり条件式を作っている
条件式はboolにしなければいけないので無理やりでも作るというトリビア

(問題)の解決
Input.GetAxisでデフォルトの←矢印をAWSDに変更することが可能なら成り立つかも。。
Aボタン押した時だけ回転させる条件を追記すれば、、、

        if (Input.GetKey(KeyCode.A))
        {
            Input.GetAxis("Horizontal");
            transform.Rotate(0, Input.GetAxis("Horizontal"), 0);
        }

とりあえず、動く感じになりました。

参考サイト;Unity 回転周りについて

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