-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathExplosive.cs
60 lines (49 loc) · 1.38 KB
/
Explosive.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
[RequireComponent(typeof(Rigidbody))]
public class Explosive : MonoBehaviour
{
public Collider[] hitColliders;
public LayerMask explosionLayers;
public int currentHealth = 10;
public float radius = 5.0f;
public float power = 10.0f;
public AudioSource explosionSound;
bool isExploded;
void Start()
{
}
void Update()
{
}
public void TakeDamage(int amount)
{
if (isExploded)
return;
currentHealth -= amount;
//hitParticles.transform.position = hitPoint;
//hitParticles.Stop();
//hitParticles.Play();
if (currentHealth <= 0)
{
Explode();
}
}
void Explode()
{
explosionSound.Stop();
explosionSound.Play();
Vector3 explosionPos = transform.position;
hitColliders = Physics.OverlapSphere(explosionPos, radius, explosionLayers);
foreach(var collider in hitColliders)
{
if(collider.GetComponent<Rigidbody>() != null)
{
collider.GetComponent<Rigidbody>().isKinematic = false;
collider.GetComponent<Rigidbody>().AddExplosionForce(power, explosionPos, radius, 3.0f, ForceMode.Impulse);
}
}
Destroy(gameObject, 0.5f);
}
}