質問編集履歴

2

呼び出し側コード追加

2021/04/27 06:21

投稿

退会済みユーザー
test CHANGED
File without changes
test CHANGED
@@ -73,3 +73,379 @@
73
73
  ```
74
74
 
75
75
  ![![イメージ説明](8a86bc35d194cf64101c6db77bf52cce.png)](ded07b86ac1a3057b505735dc1444356.png)
76
+
77
+
78
+
79
+ ```
80
+
81
+
82
+
83
+ using UnityEngine;
84
+
85
+
86
+
87
+ public class PlayerManager : MonoBehaviour
88
+
89
+ {
90
+
91
+
92
+
93
+ //public
94
+
95
+
96
+
97
+ public float xspeed;
98
+
99
+ public float jumpPower;
100
+
101
+ public GroundCheck ground;
102
+
103
+ public GameManager gameManager;
104
+
105
+ //SE
106
+
107
+ [SerializeField] AudioClip jumpSE;
108
+
109
+
110
+
111
+ public Traps trap1;
112
+
113
+ public Trap2 trap2;
114
+
115
+
116
+
117
+
118
+
119
+
120
+
121
+ //private
122
+
123
+
124
+
125
+ private bool isGround = false;
126
+
127
+ float speed;
128
+
129
+ bool isDead = false;
130
+
131
+ AudioSource audioSource;
132
+
133
+
134
+
135
+
136
+
137
+
138
+
139
+
140
+
141
+
142
+
143
+ public enum DIRECTION_TYPE
144
+
145
+ {
146
+
147
+ STOP,
148
+
149
+ RIGHT,
150
+
151
+ LEFT
152
+
153
+ }
154
+
155
+
156
+
157
+ DIRECTION_TYPE direction = DIRECTION_TYPE.STOP;
158
+
159
+
160
+
161
+ Rigidbody2D rigidbody2D;
162
+
163
+ Rigidbody2D trapSpike1;
164
+
165
+ Animator animator;
166
+
167
+
168
+
169
+
170
+
171
+ private void Start()
172
+
173
+ {
174
+
175
+ rigidbody2D = GetComponent<Rigidbody2D>();
176
+
177
+ trapSpike1 = GetComponent<Rigidbody2D>();
178
+
179
+ animator = GetComponent<Animator>();
180
+
181
+ audioSource = GetComponent<AudioSource>();
182
+
183
+ }
184
+
185
+
186
+
187
+ void Update()
188
+
189
+ {
190
+
191
+ if (isDead)
192
+
193
+ {
194
+
195
+ return;
196
+
197
+ }
198
+
199
+ //接地判定を得る
200
+
201
+ isGround = ground.IsGround();
202
+
203
+
204
+
205
+ float x = Input.GetAxis("Horizontal");
206
+
207
+
208
+
209
+ //speedというパラメータにxを代入
210
+
211
+ animator.SetFloat("speed", Mathf.Abs(x));
212
+
213
+
214
+
215
+ if (x == 0)
216
+
217
+ {
218
+
219
+ //止まっている
220
+
221
+ direction = DIRECTION_TYPE.STOP;
222
+
223
+ }
224
+
225
+ else if (x > 0)
226
+
227
+ {
228
+
229
+ //右に動く
230
+
231
+ direction = DIRECTION_TYPE.RIGHT;
232
+
233
+ }
234
+
235
+ else if (x < 0)
236
+
237
+ {
238
+
239
+ //左に動く
240
+
241
+ direction = DIRECTION_TYPE.LEFT;
242
+
243
+ }
244
+
245
+
246
+
247
+ //スペース押したらジャンプ
248
+
249
+
250
+
251
+ if (isGround)
252
+
253
+ {
254
+
255
+ if (Input.GetKeyDown("space"))
256
+
257
+ {
258
+
259
+ Jump();
260
+
261
+ }
262
+
263
+ else
264
+
265
+ {
266
+
267
+ animator.SetBool("isJumping", false);
268
+
269
+ }
270
+
271
+
272
+
273
+ }
274
+
275
+
276
+
277
+
278
+
279
+ switch (direction)
280
+
281
+ {
282
+
283
+ case DIRECTION_TYPE.STOP:
284
+
285
+ speed = 0;
286
+
287
+ break;
288
+
289
+
290
+
291
+ case DIRECTION_TYPE.RIGHT:
292
+
293
+ speed = xspeed;
294
+
295
+ transform.localScale = new Vector3(1, 1, 1);
296
+
297
+ break;
298
+
299
+
300
+
301
+ case DIRECTION_TYPE.LEFT:
302
+
303
+ speed = -xspeed;
304
+
305
+ transform.localScale = new Vector3(-1, 1, 1);
306
+
307
+ break;
308
+
309
+ }
310
+
311
+ rigidbody2D.velocity = new Vector2(speed, rigidbody2D.velocity.y);
312
+
313
+ }
314
+
315
+
316
+
317
+
318
+
319
+ void Jump()
320
+
321
+ {
322
+
323
+ //上に力を加える
324
+
325
+ rigidbody2D.AddForce(Vector2.up * jumpPower);
326
+
327
+ animator.SetBool("isJumping", true);
328
+
329
+ audioSource.PlayOneShot(jumpSE);
330
+
331
+
332
+
333
+ }
334
+
335
+
336
+
337
+ private void OnTriggerEnter2D(Collider2D collision)
338
+
339
+ {
340
+
341
+ if (isDead)
342
+
343
+ {
344
+
345
+ return;
346
+
347
+ }
348
+
349
+ if (collision.gameObject.tag == "Trap")
350
+
351
+ {
352
+
353
+ Debug.Log("GameOver");
354
+
355
+ gameManager.GameOver();
356
+
357
+ PlayerDeath();
358
+
359
+ }
360
+
361
+ if (collision.gameObject.tag == "Finish")
362
+
363
+ {
364
+
365
+ Debug.Log("GameClear");
366
+
367
+ gameManager.GameClear();
368
+
369
+ }
370
+
371
+
372
+
373
+ //トラップSwitchのタグ
374
+
375
+ if (collision.gameObject.tag == "Switch1")
376
+
377
+ {
378
+
379
+ Debug.Log("罠1起動");
380
+
381
+ trap1.Trap1Activate();
382
+
383
+
384
+
385
+
386
+
387
+ }
388
+
389
+ if (collision.gameObject.tag == "Switch2")
390
+
391
+ {
392
+
393
+ Debug.Log("罠2起動");
394
+
395
+ trap2.Trap2Activate();
396
+
397
+ }
398
+
399
+ if (collision.gameObject.tag == "Switch3")
400
+
401
+ {
402
+
403
+ Debug.Log("ブロック出現");
404
+
405
+ //trap2.Trap2Activate();
406
+
407
+
408
+
409
+
410
+
411
+
412
+
413
+ }
414
+
415
+
416
+
417
+ }
418
+
419
+ void PlayerDeath()
420
+
421
+ {
422
+
423
+ BoxCollider2D boxCollider2D = GetComponent<BoxCollider2D>();
424
+
425
+ Destroy (boxCollider2D);
426
+
427
+ rigidbody2D.velocity = new Vector2(0,0);
428
+
429
+ rigidbody2D.AddForce(Vector2.up * 400);
430
+
431
+ animator.Play("PlayerDeathAnimation");
432
+
433
+ gameManager.GameOver();
434
+
435
+ isDead = true;
436
+
437
+
438
+
439
+ }
440
+
441
+
442
+
443
+ }
444
+
445
+
446
+
447
+
448
+
449
+ コード
450
+
451
+ ```

1

図追加

2021/04/27 06:21

投稿

退会済みユーザー
test CHANGED
File without changes
test CHANGED
@@ -71,3 +71,5 @@
71
71
  コード
72
72
 
73
73
  ```
74
+
75
+ ![![イメージ説明](8a86bc35d194cf64101c6db77bf52cce.png)](ded07b86ac1a3057b505735dc1444356.png)