質問編集履歴
3
コードの区切り
test
CHANGED
File without changes
|
test
CHANGED
@@ -4,7 +4,7 @@
|
|
4
4
|
|
5
5
|
|
6
6
|
|
7
|
-
Scr_UnityChan
|
7
|
+
<Scr_UnityChan>
|
8
8
|
|
9
9
|
using UnityEngine;
|
10
10
|
|
@@ -390,9 +390,9 @@
|
|
390
390
|
|
391
391
|
}
|
392
392
|
|
393
|
-
|
393
|
+
------------------------------
|
394
|
-
|
394
|
+
|
395
|
-
Scr_Jump
|
395
|
+
<Scr_Jump>
|
396
396
|
|
397
397
|
using UnityEngine;
|
398
398
|
|
@@ -444,9 +444,9 @@
|
|
444
444
|
|
445
445
|
}
|
446
446
|
|
447
|
-
|
447
|
+
------------------------------
|
448
|
-
|
448
|
+
|
449
|
-
Scr_RightLeft
|
449
|
+
<Scr_RightLeft>
|
450
450
|
|
451
451
|
using UnityEngine;
|
452
452
|
|
@@ -528,6 +528,8 @@
|
|
528
528
|
|
529
529
|
}
|
530
530
|
|
531
|
+
------------------------------
|
532
|
+
|
531
533
|
|
532
534
|
|
533
535
|
この様にスクリプトを組んでみましたが、ボタンを押さないと飛ばず、左右移動は逐一連打しないと移動できません。
|
2
タイトル変更、タグ追加
test
CHANGED
@@ -1 +1 @@
|
|
1
|
-
ユニティ
|
1
|
+
ユニティを使用したゲームの操作
|
test
CHANGED
File without changes
|
1
スクリプトの追加、スクリプトによる動作の記入
test
CHANGED
File without changes
|
test
CHANGED
@@ -1,17 +1,543 @@
|
|
1
|
-
### 前提・実現したいこと
|
2
|
-
|
3
1
|
![イメージ説明](e94619f2dd00a132803b9a7c661b4acc.png)
|
4
2
|
|
5
|
-
現在unity3Dでゲームを製作しています。
|
3
|
+
現在unity3Dでゲームを製作しています。
|
4
|
+
|
5
|
+
|
6
|
+
|
6
|
-
|
7
|
+
Scr_UnityChan
|
8
|
+
|
9
|
+
using UnityEngine;
|
10
|
+
|
11
|
+
using UnityEngine.UI; // UI系のコンポーネントを使用する為に必要
|
12
|
+
|
13
|
+
using UnityEngine.SceneManagement; // SceneManagerを使用する為に必要
|
14
|
+
|
15
|
+
using System.Collections;
|
16
|
+
|
17
|
+
|
18
|
+
|
19
|
+
public class Scr_UnityChan : MonoBehaviour {
|
20
|
+
|
21
|
+
Animator animator;
|
22
|
+
|
23
|
+
|
24
|
+
|
25
|
+
//Rigidbody用の変数
|
26
|
+
|
27
|
+
Rigidbody rb;
|
28
|
+
|
29
|
+
|
30
|
+
|
31
|
+
// UnityのHierarchyビューにあるUnityChanから値を直接編集できるようになる
|
32
|
+
|
33
|
+
public float Speed = 0;
|
34
|
+
|
35
|
+
public float JumpPower = 0;//ユニティちゃんのジャンプ力
|
36
|
+
|
37
|
+
public Text TextScore; // ユニティちゃんからテキストオブジェクトをいじるためpublicにする
|
38
|
+
|
39
|
+
|
40
|
+
|
41
|
+
public AudioClip SeJump; // ユニティちゃんからジャンプ時のSEを設定するためpublic
|
42
|
+
|
43
|
+
|
44
|
+
|
45
|
+
bool jumpStartFlag = false; //ジャンプ開始フラグ
|
46
|
+
|
47
|
+
bool groundFlag = false; //着地フラグ
|
48
|
+
|
49
|
+
|
50
|
+
|
51
|
+
//1回前のX座標の保存用
|
52
|
+
|
53
|
+
float prevPosX = -1;
|
54
|
+
|
55
|
+
|
56
|
+
|
57
|
+
int score = 0; //ゲームのスコア
|
58
|
+
|
59
|
+
float Itemtimer = 0f;
|
60
|
+
|
61
|
+
bool useItem = false;
|
62
|
+
|
63
|
+
|
64
|
+
|
65
|
+
//実行された時に1回だけ呼ばれる
|
66
|
+
|
67
|
+
void Start() {
|
68
|
+
|
69
|
+
// unitychanのAnimatorコンポーネントをScriptで扱えるようにする
|
70
|
+
|
71
|
+
animator = GetComponent<Animator>();
|
72
|
+
|
73
|
+
// unitychanのRigidbodyコンポーネントをScriptで扱えるようにする
|
74
|
+
|
75
|
+
rb = GetComponent<Rigidbody>();
|
76
|
+
|
77
|
+
|
78
|
+
|
79
|
+
//走りモーションに移行
|
80
|
+
|
81
|
+
animator.SetBool("IsRunning", true);
|
82
|
+
|
83
|
+
}
|
84
|
+
|
85
|
+
|
86
|
+
|
87
|
+
//1F(1/60秒)毎に呼ばれる(入力系の処理)
|
88
|
+
|
89
|
+
void Update()
|
90
|
+
|
91
|
+
{
|
92
|
+
|
93
|
+
|
94
|
+
|
95
|
+
if (useItem == true)
|
96
|
+
|
97
|
+
{
|
98
|
+
|
99
|
+
Itemtimer -= Time.deltaTime;
|
100
|
+
|
101
|
+
if (Itemtimer <= 0)
|
102
|
+
|
103
|
+
{
|
104
|
+
|
105
|
+
useItem = false;
|
106
|
+
|
107
|
+
Speed = 4;
|
108
|
+
|
109
|
+
}
|
110
|
+
|
111
|
+
}
|
112
|
+
|
113
|
+
|
114
|
+
|
115
|
+
}
|
116
|
+
|
117
|
+
|
118
|
+
|
119
|
+
|
120
|
+
|
121
|
+
// 1F(1/60秒)毎に呼ばれる(物理演算系の処理)
|
122
|
+
|
123
|
+
void FixedUpdate()
|
124
|
+
|
125
|
+
{
|
126
|
+
|
127
|
+
//ユニティちゃんの現在のY座標が0でない
|
128
|
+
|
129
|
+
if (transform.position.y == 0 || transform.position.y == 0.6899)
|
130
|
+
|
131
|
+
{
|
132
|
+
|
133
|
+
}
|
134
|
+
|
135
|
+
//ユニティちゃんの現在のY座標が0
|
136
|
+
|
137
|
+
else
|
138
|
+
|
139
|
+
{
|
140
|
+
|
141
|
+
// 落ちたら
|
142
|
+
|
143
|
+
if (transform.position.y <= -5)
|
144
|
+
|
145
|
+
{
|
146
|
+
|
147
|
+
//ゲームオーバーシーンへ移行
|
148
|
+
|
149
|
+
SceneManager.LoadScene("UnityChanRun_GameOver");
|
150
|
+
|
151
|
+
}
|
152
|
+
|
153
|
+
|
154
|
+
|
155
|
+
}
|
156
|
+
|
157
|
+
|
158
|
+
|
159
|
+
//jumpStartFlagがtrue?
|
160
|
+
|
161
|
+
if (jumpStartFlag == true)
|
162
|
+
|
163
|
+
{
|
164
|
+
|
165
|
+
//ジャンプモーションに遷移
|
166
|
+
|
167
|
+
animator.SetBool("IsJump", true);
|
168
|
+
|
169
|
+
|
170
|
+
|
171
|
+
//上方に速度を加える
|
172
|
+
|
173
|
+
rb.AddForce(Vector3.up * JumpPower, ForceMode.VelocityChange);
|
174
|
+
|
175
|
+
|
176
|
+
|
177
|
+
jumpStartFlag = false;
|
178
|
+
|
179
|
+
}
|
180
|
+
|
181
|
+
|
182
|
+
|
183
|
+
//前回のX座標と現在のX座標が同じでないなら
|
184
|
+
|
185
|
+
if (transform.position.x != prevPosX)
|
186
|
+
|
187
|
+
{
|
188
|
+
|
189
|
+
// x軸方向に進むようにする。 ※yとzを0にしないように注意
|
190
|
+
|
191
|
+
rb.velocity = new Vector3(Speed, rb.velocity.y, rb.velocity.z);
|
192
|
+
|
193
|
+
}
|
194
|
+
|
195
|
+
//X座標の保存
|
196
|
+
|
197
|
+
prevPosX = transform.position.x;
|
198
|
+
|
199
|
+
}
|
200
|
+
|
201
|
+
|
202
|
+
|
203
|
+
//物体と物体が触れたときに呼ばれる
|
204
|
+
|
205
|
+
void OnCollisionEnter(Collision collision) {
|
206
|
+
|
207
|
+
//ジャンプモーションのフラグをfalseにする
|
208
|
+
|
209
|
+
animator.SetBool("IsJump", false);
|
210
|
+
|
211
|
+
groundFlag = true;
|
212
|
+
|
213
|
+
}
|
214
|
+
|
215
|
+
|
216
|
+
|
217
|
+
//物体と物体が触れたときに呼ばれる
|
218
|
+
|
219
|
+
void OnCollisionExit(Collision collision)
|
220
|
+
|
221
|
+
{
|
222
|
+
|
223
|
+
groundFlag = false;
|
224
|
+
|
225
|
+
}
|
226
|
+
|
227
|
+
|
228
|
+
|
229
|
+
//物体と物体が触れたときに呼ばれる(IsTrigger有効時)
|
230
|
+
|
231
|
+
void OnTriggerEnter(Collider other) {
|
232
|
+
|
233
|
+
// 衝突したGameObjectのタグが「goal」であるか
|
234
|
+
|
235
|
+
if (other.tag == "goal")
|
236
|
+
|
237
|
+
{
|
238
|
+
|
239
|
+
|
240
|
+
|
241
|
+
// IsJumpとIsRunningをfalseにし、
|
242
|
+
|
243
|
+
// IsGoalをtrueにする
|
244
|
+
|
245
|
+
animator.SetBool("IsJump", false);
|
246
|
+
|
247
|
+
animator.SetBool("IsRunning", false);
|
248
|
+
|
249
|
+
animator.SetBool("IsGoal", true);
|
250
|
+
|
251
|
+
|
252
|
+
|
253
|
+
// 止まらせる
|
254
|
+
|
255
|
+
Speed = 0;
|
256
|
+
|
257
|
+
JumpPower = 0;
|
258
|
+
|
259
|
+
|
260
|
+
|
261
|
+
// UnityChanを00度回転させて、こっちを向かせる
|
262
|
+
|
263
|
+
transform.Rotate(0, 180, 0);
|
264
|
+
|
265
|
+
|
266
|
+
|
267
|
+
// GameObject Goalを消滅させる
|
268
|
+
|
269
|
+
Destroy(other.gameObject);
|
270
|
+
|
271
|
+
}
|
272
|
+
|
273
|
+
}
|
274
|
+
|
275
|
+
|
276
|
+
|
277
|
+
public void Jump()
|
278
|
+
|
279
|
+
{
|
280
|
+
|
281
|
+
if (groundFlag == true)
|
282
|
+
|
283
|
+
{
|
284
|
+
|
285
|
+
//マウス左ボタン押したら(Androidの場合はタップ)
|
286
|
+
|
287
|
+
// if (Input.GetMouseButtonDown(0) == true)
|
288
|
+
|
289
|
+
{
|
290
|
+
|
291
|
+
//ジャンプ用のフラグを立てる
|
292
|
+
|
293
|
+
jumpStartFlag = true;
|
294
|
+
|
295
|
+
|
296
|
+
|
297
|
+
// SEを鳴らす
|
298
|
+
|
299
|
+
GetComponent<AudioSource>().PlayOneShot(SeJump);
|
300
|
+
|
301
|
+
}
|
302
|
+
|
303
|
+
}
|
304
|
+
|
305
|
+
Debug.Log("lllllll");
|
306
|
+
|
307
|
+
}
|
308
|
+
|
309
|
+
|
310
|
+
|
311
|
+
//ユニティちゃんのスコア加算用関数
|
312
|
+
|
313
|
+
public void AddScore(int value) {
|
314
|
+
|
315
|
+
//スコアに加算する
|
316
|
+
|
317
|
+
score = score + value;
|
318
|
+
|
319
|
+
//Consoleに出力する
|
320
|
+
|
321
|
+
Debug.Log("Score = " + score);
|
322
|
+
|
323
|
+
|
324
|
+
|
325
|
+
// Score:~を表示
|
326
|
+
|
327
|
+
TextScore.text = "Score:" + score.ToString();
|
328
|
+
|
329
|
+
|
330
|
+
|
331
|
+
}
|
332
|
+
|
333
|
+
|
334
|
+
|
335
|
+
//ユニティちゃんのスピード加算関数
|
336
|
+
|
337
|
+
public void SpeedItem(int value)
|
338
|
+
|
339
|
+
{
|
340
|
+
|
7
|
-
|
341
|
+
//スピードを加算する
|
342
|
+
|
343
|
+
if (useItem == false)
|
344
|
+
|
345
|
+
{
|
346
|
+
|
347
|
+
Speed = Speed + value;
|
348
|
+
|
349
|
+
//Consoleに出力する
|
350
|
+
|
351
|
+
Debug.Log("Speed = " + score);
|
352
|
+
|
353
|
+
|
354
|
+
|
355
|
+
Itemtimer = 3f;
|
356
|
+
|
357
|
+
useItem = true;
|
358
|
+
|
359
|
+
}
|
360
|
+
|
361
|
+
}
|
362
|
+
|
363
|
+
//ユニティちゃんのジャンプ加算関数
|
364
|
+
|
365
|
+
public void JumpItem(int value)
|
366
|
+
|
367
|
+
{
|
368
|
+
|
369
|
+
//ジャンプを加算する
|
370
|
+
|
371
|
+
if (useItem == false)
|
372
|
+
|
373
|
+
{
|
374
|
+
|
375
|
+
Speed = Speed + value;
|
376
|
+
|
377
|
+
//Consoleに出力する
|
378
|
+
|
379
|
+
Debug.Log("Jump = " + score);
|
380
|
+
|
381
|
+
|
382
|
+
|
383
|
+
Itemtimer = 3f;
|
384
|
+
|
385
|
+
useItem = true;
|
386
|
+
|
387
|
+
}
|
388
|
+
|
389
|
+
}
|
390
|
+
|
391
|
+
}
|
392
|
+
|
393
|
+
|
394
|
+
|
395
|
+
Scr_Jump
|
396
|
+
|
397
|
+
using UnityEngine;
|
398
|
+
|
399
|
+
using System.Collections;
|
400
|
+
|
401
|
+
|
402
|
+
|
403
|
+
public class Scr_Jump : MonoBehaviour {
|
404
|
+
|
405
|
+
public int Jump = 0; //ジャンプ
|
406
|
+
|
407
|
+
|
408
|
+
|
409
|
+
//実行された時に1回だけ呼ばれる
|
410
|
+
|
411
|
+
void Start(){
|
412
|
+
|
413
|
+
}
|
414
|
+
|
415
|
+
|
416
|
+
|
417
|
+
//1F(1/60秒)毎に呼ばれる
|
418
|
+
|
419
|
+
void Update(){
|
420
|
+
|
421
|
+
}
|
422
|
+
|
423
|
+
|
424
|
+
|
425
|
+
//物体と物体が触れたときに呼ばれる
|
426
|
+
|
427
|
+
void OnTriggerEnter(Collider other){
|
428
|
+
|
429
|
+
//Componentを取得
|
430
|
+
|
431
|
+
Scr_UnityChan unitychan = other.gameObject.GetComponent<Scr_UnityChan>();
|
432
|
+
|
433
|
+
// Scr_UnityChanのAddScore呼び出し
|
434
|
+
|
435
|
+
unitychan.JumpItem(Jump);
|
436
|
+
|
437
|
+
|
438
|
+
|
439
|
+
//アイテムの削除
|
440
|
+
|
441
|
+
Destroy(gameObject);
|
442
|
+
|
443
|
+
}
|
444
|
+
|
445
|
+
}
|
446
|
+
|
447
|
+
|
448
|
+
|
449
|
+
Scr_RightLeft
|
450
|
+
|
451
|
+
using UnityEngine;
|
452
|
+
|
453
|
+
using System.Collections;
|
454
|
+
|
455
|
+
|
456
|
+
|
457
|
+
public class Scr_RightLeft : MonoBehaviour {
|
458
|
+
|
459
|
+
public GameObject unuty;
|
460
|
+
|
461
|
+
public Vector3 SPEED = new Vector3(0.05f, 0.05f, 0.05f);
|
462
|
+
|
463
|
+
// Use this for initialization
|
464
|
+
|
465
|
+
void Start(){
|
466
|
+
|
467
|
+
}
|
468
|
+
|
469
|
+
|
470
|
+
|
471
|
+
// Update is called once per frame
|
472
|
+
|
473
|
+
void Update()
|
474
|
+
|
475
|
+
{
|
476
|
+
|
477
|
+
|
478
|
+
|
479
|
+
}
|
480
|
+
|
481
|
+
public void MoveLeft()
|
482
|
+
|
483
|
+
{
|
484
|
+
|
485
|
+
// 現在位置をPositionに代入
|
486
|
+
|
487
|
+
Vector3 Position = unuty.transform.position;
|
488
|
+
|
489
|
+
// if (Input.GetKey("left"))// 左キーを押し続けていたら
|
490
|
+
|
491
|
+
{
|
492
|
+
|
493
|
+
// 代入したPositionに対して加算減算を行う
|
494
|
+
|
495
|
+
Position.z += SPEED.z;
|
496
|
+
|
497
|
+
}
|
498
|
+
|
499
|
+
// 現在の位置に加算減算を行ったPositionを代入する
|
500
|
+
|
501
|
+
unuty.transform.position = Position;
|
502
|
+
|
503
|
+
}
|
504
|
+
|
505
|
+
public void MoveRight()
|
506
|
+
|
507
|
+
{
|
508
|
+
|
509
|
+
// 現在位置をPositionに代入
|
510
|
+
|
511
|
+
Vector3 Position = unuty.transform.position;
|
512
|
+
|
513
|
+
// if (Input.GetKey("right")) // 右キーを押し続けていたら
|
514
|
+
|
515
|
+
{
|
516
|
+
|
517
|
+
// 代入したPositionに対して加算減算を行う
|
518
|
+
|
519
|
+
Position.z -= SPEED.z;
|
520
|
+
|
521
|
+
}
|
522
|
+
|
523
|
+
// 現在の位置に加算減算を行ったPositionを代入する
|
524
|
+
|
525
|
+
unuty.transform.position = Position;
|
526
|
+
|
527
|
+
}
|
528
|
+
|
529
|
+
}
|
530
|
+
|
531
|
+
|
532
|
+
|
533
|
+
この様にスクリプトを組んでみましたが、ボタンを押さないと飛ばず、左右移動は逐一連打しないと移動できません。
|
8
534
|
|
9
535
|
|
10
536
|
|
11
537
|
・右のボタンを押している間は右に、左のボタンを押している間は左に移動する。
|
12
538
|
|
13
|
-
・
|
539
|
+
・画面のどこかをタップ、尚且つ地面に接している時のみジャンプを行う。
|
14
|
-
|
15
|
-
|
16
|
-
|
540
|
+
|
541
|
+
|
542
|
+
|
17
|
-
上記の二つの動作をさせるためにはどのよう
|
543
|
+
上記の二つの動作をさせるためにはどのようなスクリプトを組めばいいのでしょうか?
|