質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.48%
C#

C#はマルチパラダイムプログラミング言語の1つで、命令形・宣言型・関数型・ジェネリック型・コンポーネント指向・オブジェクティブ指向のプログラミング開発すべてに対応しています。

Unity

Unityは、Unity Technologiesが開発・販売している、IDEを内蔵するゲームエンジンです。主にC#を用いたプログラミングでコンテンツの開発が可能です。

Q&A

0回答

2062閲覧

UNITY ジャイロセンサーとメインカメラの同期

Qoo

総合スコア1249

C#

C#はマルチパラダイムプログラミング言語の1つで、命令形・宣言型・関数型・ジェネリック型・コンポーネント指向・オブジェクティブ指向のプログラミング開発すべてに対応しています。

Unity

Unityは、Unity Technologiesが開発・販売している、IDEを内蔵するゲームエンジンです。主にC#を用いたプログラミングでコンテンツの開発が可能です。

0グッド

0クリップ

投稿2020/01/15 06:10

編集2020/01/15 06:36

下記のサイト参考のまま、startシーンとrunシーンを作成し、
StartCameraController.csとCameraController.csをそれぞれのシーンの
main cameraにアタッチしました。

https://qiita.com/fumishitan/items/c2b023b9b9655d0a7d46

iPhoneでビルドして確認したところ、startシーンではジャイロからの値も表示され、
デバイスを傾けるとメインカメラと同期されていることが確認できます。
しかし、シーンを切り替えrunシーンになるとジャイロからの値は表示されているものの
カメラが動きません。

何が悪いのでしょうか。。

unity:2014.4.12f
iPhone:13.3

startCameraController

1using UnityEngine; 2using System.Collections; 3using UnityEngine.SceneManagement; 4 5public class startCameraController : MonoBehaviour 6{ 7 private GUIStyle labelStyle; 8 public static Quaternion ini_gyro; 9 void Start() 10 { 11 this.labelStyle = new GUIStyle(); 12 this.labelStyle.fontSize = Screen.height / 22; 13 this.labelStyle.normal.textColor = Color.white; 14 } 15 16 void Update() 17 { 18 Input.gyro.enabled = true; 19 if (Input.gyro.enabled) 20 { 21 ini_gyro = Input.gyro.attitude; 22 this.transform.localRotation = Quaternion.Euler(90, 0, 0) * (new Quaternion(-ini_gyro.x, -ini_gyro.y, ini_gyro.z, ini_gyro.w)); 23 } 24 } 25 26 //ジャイロセンサの値を表示するプログラム 27 void OnGUI() 28 { 29 if (Input.gyro.enabled) 30 { 31 ini_gyro = Quaternion.Euler(90, 0, 0) * (new Quaternion(-ini_gyro.x, -ini_gyro.y, ini_gyro.z, ini_gyro.w)); 32 float x = Screen.width / 10; 33 float y = 0; 34 float w = Screen.width * 8 / 10; 35 float h = Screen.height / 20; 36 37 for (int i = 0; i < 3; i++) 38 { 39 y = Screen.height / 10 + h * i; 40 string text = string.Empty; 41 42 switch (i) 43 { 44 case 0://X 45 text = string.Format("gyro-X:{0}", ini_gyro.x); 46 break; 47 case 1://Y 48 text = string.Format("gyro-Y:{0}", ini_gyro.y); 49 break; 50 case 2://Z 51 text = string.Format("gyro-Z:{0}", ini_gyro.z); 52 break; 53 default: 54 throw new System.InvalidOperationException(); 55 } 56 GUI.Label(new Rect(x, y, w, h), text, this.labelStyle); 57 } 58 59 } 60 61 //スタートからシーン遷移を行う 62 if (GUI.Button(new Rect(Screen.width / 2 - Screen.width / 10, Screen.height / 2, Screen.width / 5, Screen.height / 10), "Start")) 63 { 64 SceneManager.LoadScene("run"); 65 } 66 67 } 68}

CameraController

1using UnityEngine; 2using System.Collections; 3 4public class CameraController : MonoBehaviour 5{ 6 private GUIStyle labelStyle; 7 Quaternion start_gyro; 8 Quaternion gyro; 9 void Start() 10 { 11 this.labelStyle = new GUIStyle(); 12 this.labelStyle.fontSize = Screen.height / 22; 13 this.labelStyle.normal.textColor = Color.white; 14 15 //後述するがここで「Start」シーンのジャイロの値を取っている 16 start_gyro = startCameraController.ini_gyro; 17 18 } 19 20 21 void Update() 22 { 23 Input.gyro.enabled = true; 24 if (Input.gyro.enabled) 25 { 26 gyro = Input.gyro.attitude; 27 gyro = Quaternion.Euler(90, 0, 0) * (new Quaternion(-gyro.x, -gyro.y, gyro.z, gyro.w)); 28 this.transform.localRotation = gyro; 29 //最初に見ていた向きとゲームの進行方向を合わせる 30 this.transform.localRotation = Quaternion.Euler(0, -start_gyro.y, 0); 31 } 32 } 33 34 //ジャイロセンサの値を表示するプログラム 35 void OnGUI() 36 { 37 if (Input.gyro.enabled) 38 { 39 float x = Screen.width / 10; 40 float y = 0; 41 float w = Screen.width * 8 / 10; 42 float h = Screen.height / 20; 43 44 for (int i = 0; i < 3; i++) 45 { 46 y = Screen.height / 10 + h * i; 47 string text = string.Empty; 48 49 switch (i) 50 { 51 case 0://X 52 text = string.Format("gyro-X:{0}", gyro.x); 53 break; 54 case 1://Y 55 text = string.Format("gyro-Y:{0}", gyro.y); 56 break; 57 case 2://Z 58 text = string.Format("gyro-Z:{0}", gyro.z); 59 break; 60 default: 61 throw new System.InvalidOperationException(); 62 } 63 64 GUI.Label(new Rect(x, y, w, h), text, this.labelStyle); 65 } 66 } 67 } 68}

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

y_waiwai

2020/01/15 06:12

実際のコードを提示しましょう
Qoo

2020/01/15 06:36

リンクにコードの記載がありますが、一応追記させて頂きました。
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

まだ回答がついていません

会員登録して回答してみよう

アカウントをお持ちの方は

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.48%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問