Unityで3Dオブジェクトの向きをスクリプトから取得する方法。

Unityで3Dオブジェクトのx軸に対する回転をスクリプトから取得したいです。
transform.localRotation.eulerAngles.xで取得するとインスペクタの値と同じ値が返るとは限らないため、他の方法を探しています。

イメージ説明

やりたいこと
画像のような立方体の矢印が上下左右どちらを向いているかによって、処理を変更したいです。
オブジェクトはゲーム中に物理的に回転することがあります。

MoveDirection MoveDirectionForRotation(float xRotation) { Debug.Log(gameObject.name + "MoveDirectionForRotation : " + xRotation.ToString()); float rot = xRotation; while (rot < 0) { rot += 360f; } while (rot >= 360) { rot -= 360f; } Debug.Log("rot = " + rot.ToString()); if (315f <= rot || rot < 45f) { return MoveDirection.Left; } else if (45f <= rot && rot < 135f) { return MoveDirection.Up; } else if (135f <= rot && rot < 225f) { return MoveDirection.Right; } else/* if (225f <= rot && rot < 315f)*/ { return MoveDirection.Down; } }

コメントを投稿

0 コメント