-
Notifications
You must be signed in to change notification settings - Fork 0
/
Missile.cs
70 lines (54 loc) · 1.52 KB
/
Missile.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
using System.Collections;
using UnityEngine;
public class Missile : MonoBehaviour
{
private Transform target;
[SerializeField]
private float speed;
[SerializeField]
private LayerMask layerMask;
[SerializeField]
private GameObject particle;
private float currentSpeed;
private IEnumerator Start()
{
SearchTarget();
while (true)
{
if (target == null)
{
DestroyThis();
yield break;
}
if (currentSpeed <= speed)
{
currentSpeed += speed * Time.deltaTime;
}
transform.position += transform.forward * currentSpeed * Time.deltaTime;
Vector3 direction = (target.position - transform.position).normalized;
transform.forward = Vector3.Lerp(transform.forward, direction, 0.25f);
yield return null;
}
}
private void OnTriggerStay(Collider other)
{
if (other.gameObject.layer == 8)
{
Destroy(other.gameObject);
DestroyThis();
}
}
private void SearchTarget()
{
Collider[] targets = Physics.OverlapSphere(transform.position, 100f, layerMask);
if (targets.Length > 0)
{
target = targets[Random.Range(0, targets.Length)].transform;
}
}
private void DestroyThis()
{
Destroy(Instantiate(particle, transform.position, Quaternion.identity), 0.7f);
Destroy(gameObject);
}
}