Imput.Compassを使い、スマホを回転して遊ぶゲームを制作しています。
スマホの傾きが例えば45度でその値を0度にして角度を求めたいのですが、
C#
1 public float compass = 0; // 基準 2 3 public float Roll = 45; // スマホの傾きを45度と仮定する(実際はInput.compass.magneticHeadingを使用) 4 5 public float Currentcompass = 0; // Rollの値をCurrentcompassにいれる 6 7 // Start is called before the first frame update 8 void Start() 9 { 10 Currentcompass = Roll; // Rollの値をCurrentcompassにいれる 11 12 } 13 14 // Update is called once per frame 15 void Update() 16 { 17 18 ///////////////////////// Rollの範囲を0~360にする (Input.compass.magneticHeadingの場合ここは消してます) 19 if (Roll < 0) 20 { 21 Roll += 360; 22 } 23 else if (Roll > 360) 24 { 25 Roll = 0; 26 } 27 ///////////////////////// 28 29 compass = Roll - Currentcompass; // (Roll - Currentcompass)を基準にする 30 Debug.Log(compass); 31 32 Roll++; //スマホを時計回りに回していると仮定してRollの値を1ずつ増やす 33 34 this.transform.rotation = Quaternion.Euler(0, 0, compass); 35 }
時計回りだと315度までうまくいきますが、316度以上360度以下だとcompassが-45度~0度の値になります。
どうすればcompassの値が0度~360度の範囲になりますか?
回答2件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2020/04/20 02:49