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

質問編集履歴

2

スクリプトファイルの更新

2019/12/17 20:19

投稿

0623_top
0623_top

スコア8

title CHANGED
File without changes
body CHANGED
@@ -17,6 +17,7 @@
17
17
  現状このような感じですが、どこが間違っているかがわからないため、とりあえずこの画像を貼らせていただきますが、もしどの辺が怪しい!などがありましたら、細かい画像などを追加致しますので、ご教示いただければ幸いです。
18
18
 
19
19
  ### スクリプト
20
+ 文字制限があるため、怪しいファイルのみ転載してます。
20
21
 
21
22
  GameManager
22
23
  ```C#
@@ -237,4 +238,95 @@
237
238
  emitterEnabled = true;
238
239
  }
239
240
  }
241
+ ```
242
+
243
+ Wall
244
+ ```C#
245
+ using System.Collections;
246
+ using System.Collections.Generic;
247
+ using UnityEngine;
248
+ public class Wall : MonoBehaviour
249
+ {
250
+ [SerializeField]
251
+ float speed = 10.0f;
252
+ [SerializeField]
253
+ float minScaleX = 4.0f;
254
+ [SerializeField]
255
+ float minScaleY = 3.0f;
256
+ [SerializeField]
257
+ float maxScaleX = 6.0f;
258
+ [SerializeField]
259
+ float maxScaleY = 7.0f;
260
+ bool inCamera = false;
261
+ Rigidbody2D rigidBody2D;
262
+ //初期化
263
+ void Start()
264
+ {
265
+ //コンポーネントを取得
266
+ rigidBody2D = GetComponent<Rigidbody2D>();
267
+ //壁の長さをランダムに変更
268
+ float scaleX = Random.Range(minScaleX, maxScaleX);
269
+ float scaleY = Random.Range(minScaleY, maxScaleY);
270
+ transform.localScale = new Vector3(scaleX, scaleY, transform.localScale.z);
271
+ }
272
+ //物理演算
273
+ void FixedUpdate()
274
+ {
275
+ rigidBody2D.velocity = Vector2.left * speed;
276
+ }
277
+ //消去処理
278
+ public void DestroyBlock()
279
+ {
280
+ Destroy(gameObject);
281
+ }
282
+ //カメラ内外の判定処理
283
+ void OnBecameVisible()
284
+ {
285
+ inCamera = true;
286
+ }
287
+ void OnBecameInvisible()
288
+ {
289
+ //一度画面内に入って、また出たときに消去する
290
+ if (inCamera)
291
+ {
292
+ DestroyBlock();
293
+ }
294
+ }
295
+ }
296
+ ```
297
+
298
+ Coin
299
+ ```C#
300
+ using System.Collections;
301
+ using System.Collections.Generic;
302
+ using UnityEngine;
303
+ public class Coin : MonoBehaviour
304
+ {
305
+ [SerializeField]
306
+ float speed = -10.0f;
307
+ Rigidbody2D rigidBody2D;
308
+ bool inCamera = false;
309
+ //初期化
310
+ void Start()
311
+ {
312
+ rigidBody2D = GetComponent<Rigidbody2D>();
313
+ }
314
+ //物理演算
315
+ void FixedUpdate()
316
+ {
317
+ rigidBody2D.velocity = new Vector2(speed, rigidBody2D.velocity.y); ;
318
+ }
319
+ //カメラ内外の判定処理
320
+ void OnBecameVisible()
321
+ {
322
+ inCamera = true;
323
+ }
324
+ void OnBecameInvisible()
325
+ {
326
+ if (inCamera)
327
+ {
328
+ Destroy(gameObject, 1.0f);
329
+ }
330
+ }
331
+ }
240
332
  ```

1

スクリプトファイル内容変更

2019/12/17 20:19

投稿

0623_top
0623_top

スコア8

title CHANGED
File without changes
body CHANGED
@@ -18,119 +18,6 @@
18
18
 
19
19
  ### スクリプト
20
20
 
21
- Player
22
- ```C#
23
- using System.Collections;
24
- using System.Collections.Generic;
25
- using UnityEngine;
26
- public class Player : MonoBehaviour
27
- {
28
- //インスペクタに表示する変数
29
- [SerializeField]
30
- float speed = 0.0f;
31
- [SerializeField]
32
- float jumpPower = 30.0f;
33
- [SerializeField]
34
- Transform groundCheck;
35
- [SerializeField]
36
- int itemScore = 500;
37
- //インスペクタには表示しない変数
38
- bool jumped = false;
39
- bool grounded = false;
40
- bool groundedPrev = false;
41
- bool inCamera = false;
42
- Animator animator;
43
- Rigidbody2D rigidBody2D;
44
- GameManager gameManager;
45
- //初期化
46
- void Start()
47
- {
48
- //コンポーネントを取得
49
- animator = GetComponent<Animator>();
50
- rigidBody2D = GetComponent<Rigidbody2D>();
51
- //GameManagerを検索し、コンポーネントを取得
52
- gameManager = GameObject.Find("GameManager").GetComponent<GameManager>();
53
- }
54
- //毎フレームの処理(一般)
55
- void Update()
56
- {
57
- //接地チェック
58
- //GroundCheckオブジェクトに床などが重なってるならtrue
59
- grounded = (Physics2D.OverlapPoint(groundCheck.position) != null) ? true : false;
60
- //接地してるかどうかの分岐
61
- if (grounded)
62
- {
63
- //接地しているならジャンプを許可
64
- if (Input.GetMouseButtonDown(0))
65
- {
66
- Jump();
67
- }
68
- }
69
- //ジャンプしてるかどうかの分岐
70
- if (jumped)
71
- {
72
- animator.SetTrigger("Jump");
73
- //ジャンプ終了(前フレームで接地してなくて、今のフレームで接地したとき)
74
- if (!groundedPrev & grounded)
75
- {
76
- jumped = false;
77
- }
78
- }
79
- else
80
- {
81
- animator.SetTrigger("Dash");
82
- }
83
- //このフレームの接地状態を保存
84
- groundedPrev = grounded ? true : false;
85
- }
86
- //毎フレームの処理(物理演算)
87
- void FixedUpdate()
88
- {
89
- if (grounded)
90
- {
91
- rigidBody2D.velocity = new Vector2(speed, rigidBody2D.velocity.y);
92
- }
93
- }
94
- //ジャンプ
95
- void Jump()
96
- {
97
- jumped = true;
98
- rigidBody2D.velocity = Vector2.up * jumpPower;
99
- }
100
- //死亡処理
101
- public void Dead()
102
- {
103
- gameManager.GameOver();
104
- rigidBody2D.Sleep();
105
- Destroy(gameObject);
106
- }
107
- //アイテムと衝突した時の処理
108
- void OnCollisionEnter2D(Collision2D other)
109
- {
110
- if (other.gameObject.tag == "Item")
111
- {
112
- //スコア加算
113
- gameManager.score += itemScore;
114
-
115
- //取ったアイテムを消す
116
- Destroy(other.gameObject);
117
- }
118
- }
119
- //カメラ内外の判定処理
120
- void OnBecameVisible()
121
- {
122
- inCamera = true;
123
- }
124
- void OnBecameInvisible()
125
- {
126
- if (inCamera)
127
- {
128
- Dead();
129
- }
130
- }
131
- }
132
- ```
133
-
134
21
  GameManager
135
22
  ```C#
136
23
  using System.Collections;
@@ -283,62 +170,71 @@
283
170
  }
284
171
  ```
285
172
 
286
- Wall
173
+ Emitter
287
174
  ```C#
288
175
  using System.Collections;
289
176
  using System.Collections.Generic;
290
177
  using UnityEngine;
291
- public class Wall : MonoBehaviour
178
+ public class Emitter : MonoBehaviour
292
179
  {
293
180
  [SerializeField]
294
- float speed = 10.0f;
181
+ float emitWaitTimeMin = 0.6f;
295
182
  [SerializeField]
296
- float minScaleX = 4.0f;
183
+ float emitWaitTimeMax = 1.0f;
297
184
  [SerializeField]
298
- float minScaleY = 3.0f;
185
+ float intervalCofficient = 0.98f; //レベルアップしたとき、生成間隔を何倍するかの係数
299
186
  [SerializeField]
300
- float maxScaleX = 6.0f;
187
+ float wallSpawnX = 13.0f;
301
188
  [SerializeField]
189
+ float wallSpawnY = -2.5f;
190
+ [SerializeField]
191
+ float coinSpawnX = 11.0f;
192
+ [SerializeField]
302
- float maxScaleY = 7.0f;
193
+ float coinSpawnY = 5.0f;
194
+ [SerializeField]
195
+ GameObject wall;
196
+ [SerializeField]
197
+ GameObject coin;
198
+ [SerializeField]
199
+ GameManager gameManager;
303
- bool inCamera = false;
200
+ bool emitterEnabled = true;
201
+ int cullentLevel;
304
- Rigidbody2D rigidBody2D;
202
+ float emitWaitTimeMinNow;
203
+ float emitWaitTimeMaxNow;
305
- //初期化
204
+ //毎フレームの処理
306
- void Start()
205
+ void Update()
307
206
  {
207
+ //タイトルまたはゲームオーバー画面
208
+ if (!gameManager.playing)
209
+ {
210
+ //初期化
211
+ cullentLevel = gameManager.level;
212
+ emitWaitTimeMinNow = emitWaitTimeMin;
213
+ emitWaitTimeMaxNow = emitWaitTimeMax;
214
+ }
308
- //コンポーネントを取得
215
+ //プレイ中の処理
309
- rigidBody2D = GetComponent<Rigidbody2D>();
216
+ if (emitterEnabled && gameManager.playing)
217
+ {
218
+ //生成処理
219
+ Instantiate(wall, new Vector3(wallSpawnX, wallSpawnY, transform.position.z), Quaternion.identity);
220
+ Instantiate(coin, new Vector3(coinSpawnX, coinSpawnY, transform.position.z), Quaternion.identity);
221
+ StartCoroutine(EmitWait());
310
- //壁の長さをランダム変更
222
+ //レベルアップ時生成速度Up
223
+ if (gameManager.level != cullentLevel)
224
+ {
225
+ emitWaitTimeMinNow *= intervalCofficient;
226
+ emitWaitTimeMaxNow *= intervalCofficient;
311
- float scaleX = Random.Range(minScaleX, maxScaleX);
227
+ cullentLevel = gameManager.level;
312
- float scaleY = Random.Range(minScaleY, maxScaleY);
228
+ }
313
- transform.localScale = new Vector3(scaleX, scaleY, transform.localScale.z);
229
+ }
314
230
  }
315
- //演算
231
+ //生成を待つ処
316
- void FixedUpdate()
232
+ IEnumerator EmitWait()
317
233
  {
234
+ emitterEnabled = false;
235
+ float emitWaitTime = Random.Range(emitWaitTimeMinNow, emitWaitTimeMaxNow);
318
- rigidBody2D.velocity = Vector2.left * speed;
236
+ yield return new WaitForSeconds(emitWaitTime);
237
+ emitterEnabled = true;
319
238
  }
320
- //消去処理
321
- public void DestroyBlock()
322
- {
323
- Destroy(gameObject);
324
- }
325
- //カメラ内外の判定処理
326
- void OnBecameVisible()
327
- {
328
- inCamera = true;
329
- }
330
- void OnBecameInvisible()
331
- {
332
- //一度画面内に入って、また出たときに消去する
333
- if (inCamera)
334
- {
335
- DestroyBlock();
336
- }
337
- }
338
239
  }
339
- ```
340
-
341
- Emitter
342
- ```C
343
- コード
344
240
  ```