BulletScript.cs
2.47 KB
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
using UnityEngine;
using System.Collections;
// ----- Low Poly FPS Pack Free Version -----
public class BulletScript : MonoBehaviour {
[Range(5, 100)]
[Tooltip("After how long time should the bullet prefab be destroyed?")]
public float destroyAfter;
[Tooltip("If enabled the bullet destroys on impact")]
public bool destroyOnImpact = false;
[Tooltip("Minimum time after impact that the bullet is destroyed")]
public float minDestroyTime;
[Tooltip("Maximum time after impact that the bullet is destroyed")]
public float maxDestroyTime;
[Header("Impact Effect Prefabs")]
public Transform [] metalImpactPrefabs;
private void Start ()
{
//Start destroy timer
StartCoroutine (DestroyAfter ());
}
//If the bullet collides with anything
private void OnCollisionEnter (Collision collision)
{
//If destroy on impact is false, start
//coroutine with random destroy timer
if (!destroyOnImpact)
{
StartCoroutine (DestroyTimer ());
}
//Otherwise, destroy bullet on impact
else
{
Destroy (gameObject);
}
//If bullet collides with "Metal" tag
if (collision.transform.tag == "Metal")
{
//Instantiate random impact prefab from array
Instantiate (metalImpactPrefabs [Random.Range
(0, metalImpactPrefabs.Length)], transform.position,
Quaternion.LookRotation (collision.contacts [0].normal));
//Destroy bullet object
Destroy(gameObject);
}
//If bullet collides with "Target" tag
if (collision.transform.tag == "Target")
{
//Toggle "isHit" on target object
collision.transform.gameObject.GetComponent
<TargetEdited>().isHit = true;
//Destroy bullet object
Destroy(gameObject);
}
//If bullet collides with "ExplosiveBarrel" tag
if (collision.transform.tag == "ExplosiveBarrel")
{
//Toggle "explode" on explosive barrel object
collision.transform.gameObject.GetComponent
<ExplosiveBarrelScript>().explode = true;
//Destroy bullet object
Destroy(gameObject);
}
}
private IEnumerator DestroyTimer ()
{
//Wait random time based on min and max values
yield return new WaitForSeconds
(Random.Range(minDestroyTime, maxDestroyTime));
//Destroy bullet object
Destroy(gameObject);
}
private IEnumerator DestroyAfter ()
{
//Wait for set amount of time
yield return new WaitForSeconds (destroyAfter);
//Destroy bullet object
Destroy (gameObject);
}
}
// ----- Low Poly FPS Pack Free Version -----