-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathDamage.cs
94 lines (80 loc) · 3.13 KB
/
Damage.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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
using UnityEngine;
using System.Collections;
/// <summary>
/// Damage class handler.
/// </summary>
public class Damage : MonoBehaviour {
public float damageAmount = 10.0f;
public bool damageOnTrigger = true;
public bool damageOnCollision = false;
public bool continuousDamage = false;
public float continuousTimeBetweenHits = 0;
// Variables dealing with exploding on impact (area of effect).
public bool destroySelfOnImpact = false;
public float delayBeforeDestroy = 0.0f;
public GameObject explosionPrefab;
private float savedTime = 0;
/// <summary>
/// Called when the Collider "other" enters the trigger.
/// Used for things like bullets, which are triggers.
/// See https://docs.unity3d.com/ScriptReference/Collider.OnTriggerEnter.html
/// </summary>
void OnTriggerEnter(Collider collision) {
if (damageOnTrigger) {
// If the player got hit with it's own bullets, ignore it.
if (this.tag == "PlayerBullet" && collision.gameObject.tag == "Player")
return;
// If the hit object has the Health script on it, deal damage.
if (collision.gameObject.GetComponent<Health> () != null) {
collision.gameObject.GetComponent<Health> ().ApplyDamage (damageAmount);
// Destroy the object whenever it hits something.
if (destroySelfOnImpact) {
Destroy (gameObject, delayBeforeDestroy);
}
// If explosion prefab is not null instantiates it.
if (explosionPrefab != null) {
Instantiate (explosionPrefab, transform.position, transform.rotation);
}
}
}
}
/// <summary>
/// Called when this collider/rigidbody has begun touching another rigidbody/collider.
/// This is used for things that explode on impact and are NOT triggers.
/// See https://docs.unity3d.com/ScriptReference/MonoBehaviour.OnCollisionEnter.html
/// </summary>
void OnCollisionEnter(Collision collision) {
if (damageOnCollision) {
// If the player got hit with it's own bullets, ignore it.
if (this.tag == "PlayerBullet" && collision.gameObject.tag == "Player")
return;
// If the hit object has the Health script on it, deal damage.
if (collision.gameObject.GetComponent<Health> () != null) {
collision.gameObject.GetComponent<Health> ().ApplyDamage (damageAmount);
// Destroy the object whenever it hits something.
if (destroySelfOnImpact) {
Destroy (gameObject, delayBeforeDestroy);
}
if (explosionPrefab != null) {
Instantiate (explosionPrefab, transform.position, transform.rotation);
}
}
}
}
/// <summary>
/// Called once per frame for every collider/rigidbody that is touching rigidbody/collider.
/// This is used for damage over time things.
/// See https://docs.unity3d.com/ScriptReference/Collider.OnCollisionStay.html
/// </summary>
void OnCollisionStay(Collision collision) {
if (continuousDamage) {
// It is only triggered if whatever it hits is the player.
if (collision.gameObject.tag == "Player" && collision.gameObject.GetComponent<Health> () != null) {
if (Time.time - savedTime >= continuousTimeBetweenHits) {
savedTime = Time.time;
collision.gameObject.GetComponent<Health> ().ApplyDamage (damageAmount);
}
}
}
}
}