質問するログイン新規登録

質問編集履歴

1

コード追記

2020/01/15 06:36

投稿

Qoo
Qoo

スコア1249

title CHANGED
File without changes
body CHANGED
@@ -12,4 +12,147 @@
12
12
  何が悪いのでしょうか。。
13
13
 
14
14
  unity:2014.4.12f
15
- iPhone:13.3
15
+ iPhone:13.3
16
+
17
+
18
+ ```startCameraController
19
+ using UnityEngine;
20
+ using System.Collections;
21
+ using UnityEngine.SceneManagement;
22
+
23
+ public class startCameraController : MonoBehaviour
24
+ {
25
+ private GUIStyle labelStyle;
26
+ public static Quaternion ini_gyro;
27
+ void Start()
28
+ {
29
+ this.labelStyle = new GUIStyle();
30
+ this.labelStyle.fontSize = Screen.height / 22;
31
+ this.labelStyle.normal.textColor = Color.white;
32
+ }
33
+
34
+ void Update()
35
+ {
36
+ Input.gyro.enabled = true;
37
+ if (Input.gyro.enabled)
38
+ {
39
+ ini_gyro = Input.gyro.attitude;
40
+ this.transform.localRotation = Quaternion.Euler(90, 0, 0) * (new Quaternion(-ini_gyro.x, -ini_gyro.y, ini_gyro.z, ini_gyro.w));
41
+ }
42
+ }
43
+
44
+ //ジャイロセンサの値を表示するプログラム
45
+ void OnGUI()
46
+ {
47
+ if (Input.gyro.enabled)
48
+ {
49
+ ini_gyro = Quaternion.Euler(90, 0, 0) * (new Quaternion(-ini_gyro.x, -ini_gyro.y, ini_gyro.z, ini_gyro.w));
50
+ float x = Screen.width / 10;
51
+ float y = 0;
52
+ float w = Screen.width * 8 / 10;
53
+ float h = Screen.height / 20;
54
+
55
+ for (int i = 0; i < 3; i++)
56
+ {
57
+ y = Screen.height / 10 + h * i;
58
+ string text = string.Empty;
59
+
60
+ switch (i)
61
+ {
62
+ case 0://X
63
+ text = string.Format("gyro-X:{0}", ini_gyro.x);
64
+ break;
65
+ case 1://Y
66
+ text = string.Format("gyro-Y:{0}", ini_gyro.y);
67
+ break;
68
+ case 2://Z
69
+ text = string.Format("gyro-Z:{0}", ini_gyro.z);
70
+ break;
71
+ default:
72
+ throw new System.InvalidOperationException();
73
+ }
74
+ GUI.Label(new Rect(x, y, w, h), text, this.labelStyle);
75
+ }
76
+
77
+ }
78
+
79
+ //スタートからシーン遷移を行う
80
+ if (GUI.Button(new Rect(Screen.width / 2 - Screen.width / 10, Screen.height / 2, Screen.width / 5, Screen.height / 10), "Start"))
81
+ {
82
+ SceneManager.LoadScene("run");
83
+ }
84
+
85
+ }
86
+ }
87
+ ```
88
+
89
+ ```CameraController
90
+ using UnityEngine;
91
+ using System.Collections;
92
+
93
+ public class CameraController : MonoBehaviour
94
+ {
95
+ private GUIStyle labelStyle;
96
+ Quaternion start_gyro;
97
+ Quaternion gyro;
98
+ void Start()
99
+ {
100
+ this.labelStyle = new GUIStyle();
101
+ this.labelStyle.fontSize = Screen.height / 22;
102
+ this.labelStyle.normal.textColor = Color.white;
103
+
104
+ //後述するがここで「Start」シーンのジャイロの値を取っている
105
+ start_gyro = startCameraController.ini_gyro;
106
+
107
+ }
108
+
109
+
110
+ void Update()
111
+ {
112
+ Input.gyro.enabled = true;
113
+ if (Input.gyro.enabled)
114
+ {
115
+ gyro = Input.gyro.attitude;
116
+ gyro = Quaternion.Euler(90, 0, 0) * (new Quaternion(-gyro.x, -gyro.y, gyro.z, gyro.w));
117
+ this.transform.localRotation = gyro;
118
+ //最初に見ていた向きとゲームの進行方向を合わせる
119
+ this.transform.localRotation = Quaternion.Euler(0, -start_gyro.y, 0);
120
+ }
121
+ }
122
+
123
+ //ジャイロセンサの値を表示するプログラム
124
+ void OnGUI()
125
+ {
126
+ if (Input.gyro.enabled)
127
+ {
128
+ float x = Screen.width / 10;
129
+ float y = 0;
130
+ float w = Screen.width * 8 / 10;
131
+ float h = Screen.height / 20;
132
+
133
+ for (int i = 0; i < 3; i++)
134
+ {
135
+ y = Screen.height / 10 + h * i;
136
+ string text = string.Empty;
137
+
138
+ switch (i)
139
+ {
140
+ case 0://X
141
+ text = string.Format("gyro-X:{0}", gyro.x);
142
+ break;
143
+ case 1://Y
144
+ text = string.Format("gyro-Y:{0}", gyro.y);
145
+ break;
146
+ case 2://Z
147
+ text = string.Format("gyro-Z:{0}", gyro.z);
148
+ break;
149
+ default:
150
+ throw new System.InvalidOperationException();
151
+ }
152
+
153
+ GUI.Label(new Rect(x, y, w, h), text, this.labelStyle);
154
+ }
155
+ }
156
+ }
157
+ }
158
+ ```