質問編集履歴

1

チュートリアルの¥スクリプトの追加をしました。

2021/12/24 11:31

投稿

tr1996
tr1996

スコア1

test CHANGED
File without changes
test CHANGED
@@ -222,6 +222,156 @@
222
222
 
223
223
  }
224
224
 
225
+
226
+
227
+ ```
228
+
229
+
230
+
231
+ ```
232
+
233
+ チュートリアルのスクリプト
234
+
235
+ using UnityEngine;
236
+
237
+
238
+
239
+ public class ShellExplosion : MonoBehaviour
240
+
241
+ {
242
+
243
+ public LayerMask m_TankMask;
244
+
245
+ public ParticleSystem m_ExplosionParticles;
246
+
247
+ public AudioSource m_ExplosionAudio;
248
+
249
+ public float m_MaxDamage = 100f;
250
+
251
+ public float m_ExplosionForce = 1000f;
252
+
253
+ public float m_MaxLifeTime = 2f;
254
+
255
+ public float m_ExplosionRadius = 5f;
256
+
257
+
258
+
259
+
260
+
261
+ private void Start()
262
+
263
+ {
264
+
265
+ Destroy(gameObject, m_MaxLifeTime);
266
+
267
+ }
268
+
269
+
270
+
271
+
272
+
273
+ private void OnTriggerEnter(Collider other)
274
+
275
+ {
276
+
277
+ // Find all the tanks in an area around the shell and damage them.
278
+
279
+ Collider[] colliders = Physics.OverlapSphere(transform.position, m_ExplosionRadius, m_TankMask);
280
+
281
+
282
+
283
+ for (int i = 0; i < colliders.Length; i++)
284
+
285
+ {
286
+
287
+ Rigidbody targetRigidbody = colliders[i].GetComponent<Rigidbody>();
288
+
289
+
290
+
291
+ if (!targetRigidbody)
292
+
293
+ continue;
294
+
295
+
296
+
297
+ targetRigidbody.AddExplosionForce(m_ExplosionForce, transform.position, m_ExplosionRadius);
298
+
299
+
300
+
301
+ TankHealth targetHealth = targetRigidbody.GetComponent<TankHealth>();
302
+
303
+
304
+
305
+ if (!targetHealth)
306
+
307
+ continue;
308
+
309
+
310
+
311
+ float damage = CalculateDamage(targetRigidbody.position);
312
+
313
+
314
+
315
+ targetHealth.TakeDamage(damage);
316
+
317
+ }
318
+
319
+
320
+
321
+ m_ExplosionParticles.transform.parent = null;
322
+
323
+
324
+
325
+ m_ExplosionParticles.Play();
326
+
327
+
328
+
329
+ m_ExplosionAudio.Play();
330
+
331
+
332
+
333
+ Destroy (m_ExplosionParticles.gameObject, m_ExplosionParticles.main.duration);
334
+
335
+ Destroy (gameObject);
336
+
337
+ }
338
+
339
+
340
+
341
+
342
+
343
+ private float CalculateDamage(Vector3 targetPosition)
344
+
345
+ {
346
+
347
+ // Calculate the amount of damage a target should take based on it's position.
348
+
349
+ Vector3 explosionToTarget = targetPosition - transform.position;
350
+
351
+
352
+
353
+ float explosionDistance = explosionToTarget.magnitude;
354
+
355
+
356
+
357
+ float relativeDistance = (m_ExplosionRadius - explosionDistance) / m_ExplosionRadius;
358
+
359
+
360
+
361
+ float damage = relativeDistance * m_MaxDamage;
362
+
363
+
364
+
365
+ damage = Mathf.Max(0f, damage);
366
+
367
+
368
+
369
+ return damage;
370
+
371
+ }
372
+
373
+ }
374
+
225
375
  ```
226
376
 
227
377