質問編集履歴

1

コード追記

2020/01/15 06:36

投稿

Qoo
Qoo

スコア1249

test CHANGED
File without changes
test CHANGED
@@ -27,3 +27,289 @@
27
27
  unity:2014.4.12f
28
28
 
29
29
  iPhone:13.3
30
+
31
+
32
+
33
+
34
+
35
+ ```startCameraController
36
+
37
+ using UnityEngine;
38
+
39
+ using System.Collections;
40
+
41
+ using UnityEngine.SceneManagement;
42
+
43
+
44
+
45
+ public class startCameraController : MonoBehaviour
46
+
47
+ {
48
+
49
+ private GUIStyle labelStyle;
50
+
51
+ public static Quaternion ini_gyro;
52
+
53
+ void Start()
54
+
55
+ {
56
+
57
+ this.labelStyle = new GUIStyle();
58
+
59
+ this.labelStyle.fontSize = Screen.height / 22;
60
+
61
+ this.labelStyle.normal.textColor = Color.white;
62
+
63
+ }
64
+
65
+
66
+
67
+ void Update()
68
+
69
+ {
70
+
71
+ Input.gyro.enabled = true;
72
+
73
+ if (Input.gyro.enabled)
74
+
75
+ {
76
+
77
+ ini_gyro = Input.gyro.attitude;
78
+
79
+ this.transform.localRotation = Quaternion.Euler(90, 0, 0) * (new Quaternion(-ini_gyro.x, -ini_gyro.y, ini_gyro.z, ini_gyro.w));
80
+
81
+ }
82
+
83
+ }
84
+
85
+
86
+
87
+ //ジャイロセンサの値を表示するプログラム
88
+
89
+ void OnGUI()
90
+
91
+ {
92
+
93
+ if (Input.gyro.enabled)
94
+
95
+ {
96
+
97
+ ini_gyro = Quaternion.Euler(90, 0, 0) * (new Quaternion(-ini_gyro.x, -ini_gyro.y, ini_gyro.z, ini_gyro.w));
98
+
99
+ float x = Screen.width / 10;
100
+
101
+ float y = 0;
102
+
103
+ float w = Screen.width * 8 / 10;
104
+
105
+ float h = Screen.height / 20;
106
+
107
+
108
+
109
+ for (int i = 0; i < 3; i++)
110
+
111
+ {
112
+
113
+ y = Screen.height / 10 + h * i;
114
+
115
+ string text = string.Empty;
116
+
117
+
118
+
119
+ switch (i)
120
+
121
+ {
122
+
123
+ case 0://X
124
+
125
+ text = string.Format("gyro-X:{0}", ini_gyro.x);
126
+
127
+ break;
128
+
129
+ case 1://Y
130
+
131
+ text = string.Format("gyro-Y:{0}", ini_gyro.y);
132
+
133
+ break;
134
+
135
+ case 2://Z
136
+
137
+ text = string.Format("gyro-Z:{0}", ini_gyro.z);
138
+
139
+ break;
140
+
141
+ default:
142
+
143
+ throw new System.InvalidOperationException();
144
+
145
+ }
146
+
147
+ GUI.Label(new Rect(x, y, w, h), text, this.labelStyle);
148
+
149
+ }
150
+
151
+
152
+
153
+ }
154
+
155
+
156
+
157
+ //スタートからシーン遷移を行う
158
+
159
+ if (GUI.Button(new Rect(Screen.width / 2 - Screen.width / 10, Screen.height / 2, Screen.width / 5, Screen.height / 10), "Start"))
160
+
161
+ {
162
+
163
+ SceneManager.LoadScene("run");
164
+
165
+ }
166
+
167
+
168
+
169
+ }
170
+
171
+ }
172
+
173
+ ```
174
+
175
+
176
+
177
+ ```CameraController
178
+
179
+ using UnityEngine;
180
+
181
+ using System.Collections;
182
+
183
+
184
+
185
+ public class CameraController : MonoBehaviour
186
+
187
+ {
188
+
189
+ private GUIStyle labelStyle;
190
+
191
+ Quaternion start_gyro;
192
+
193
+ Quaternion gyro;
194
+
195
+ void Start()
196
+
197
+ {
198
+
199
+ this.labelStyle = new GUIStyle();
200
+
201
+ this.labelStyle.fontSize = Screen.height / 22;
202
+
203
+ this.labelStyle.normal.textColor = Color.white;
204
+
205
+
206
+
207
+ //後述するがここで「Start」シーンのジャイロの値を取っている
208
+
209
+ start_gyro = startCameraController.ini_gyro;
210
+
211
+
212
+
213
+ }
214
+
215
+
216
+
217
+
218
+
219
+ void Update()
220
+
221
+ {
222
+
223
+ Input.gyro.enabled = true;
224
+
225
+ if (Input.gyro.enabled)
226
+
227
+ {
228
+
229
+ gyro = Input.gyro.attitude;
230
+
231
+ gyro = Quaternion.Euler(90, 0, 0) * (new Quaternion(-gyro.x, -gyro.y, gyro.z, gyro.w));
232
+
233
+ this.transform.localRotation = gyro;
234
+
235
+ //最初に見ていた向きとゲームの進行方向を合わせる
236
+
237
+ this.transform.localRotation = Quaternion.Euler(0, -start_gyro.y, 0);
238
+
239
+ }
240
+
241
+ }
242
+
243
+
244
+
245
+ //ジャイロセンサの値を表示するプログラム
246
+
247
+ void OnGUI()
248
+
249
+ {
250
+
251
+ if (Input.gyro.enabled)
252
+
253
+ {
254
+
255
+ float x = Screen.width / 10;
256
+
257
+ float y = 0;
258
+
259
+ float w = Screen.width * 8 / 10;
260
+
261
+ float h = Screen.height / 20;
262
+
263
+
264
+
265
+ for (int i = 0; i < 3; i++)
266
+
267
+ {
268
+
269
+ y = Screen.height / 10 + h * i;
270
+
271
+ string text = string.Empty;
272
+
273
+
274
+
275
+ switch (i)
276
+
277
+ {
278
+
279
+ case 0://X
280
+
281
+ text = string.Format("gyro-X:{0}", gyro.x);
282
+
283
+ break;
284
+
285
+ case 1://Y
286
+
287
+ text = string.Format("gyro-Y:{0}", gyro.y);
288
+
289
+ break;
290
+
291
+ case 2://Z
292
+
293
+ text = string.Format("gyro-Z:{0}", gyro.z);
294
+
295
+ break;
296
+
297
+ default:
298
+
299
+ throw new System.InvalidOperationException();
300
+
301
+ }
302
+
303
+
304
+
305
+ GUI.Label(new Rect(x, y, w, h), text, this.labelStyle);
306
+
307
+ }
308
+
309
+ }
310
+
311
+ }
312
+
313
+ }
314
+
315
+ ```