-
Notifications
You must be signed in to change notification settings - Fork 0
/
WanderingNPC.cs
129 lines (99 loc) · 3.15 KB
/
WanderingNPC.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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.AI;
//Similar to WanderingAI except these NPC dont shoot the just turn around and run qickly
public class WanderingNPC : MonoBehaviour {
public float wanderRadius;
public float wanderTimer;
private Transform target;
private NavMeshAgent agent;
private float timer;
private Transform player;
private Vector3 a;
private Vector3 b;
private Vector3 c;
private Vector3 d;
private Vector3 dest;
private float dis;
private bool _alive;
private bool running;
void Start() {
_alive = true;
player = GameObject.FindGameObjectWithTag ("Player").transform;
agent = GetComponent<NavMeshAgent> ();
timer = wanderTimer;
running = false;
}
void Update() {
if (_alive) {
if (transform.position.y < -1) {
Destroy(this.gameObject);
}
float distance = Vector3.Distance (player.transform.position, transform.position);
if (distance <= 10.0f) {
Vector3 lookVector = player.transform.position - transform.position;
lookVector.y = transform.position.y;
Quaternion rot = Quaternion.LookRotation (-lookVector);
transform.rotation = Quaternion.Slerp (transform.rotation, rot, 1);
if (!running) {
NavMeshAgent agent = GetComponent<NavMeshAgent> ();
a = new Vector3 (-20.89f, 0.39f, 23.703f);
b = new Vector3 (19.37f, 0.39f, -36.61f);
c = new Vector3 (-22.79f, 0.39f,-34.62f);
d = new Vector3 (2.56f, 0.3f, 37.84f);
float distance1 = Vector3.Distance (a, transform.position);
float distance2 = Vector3.Distance (b, transform.position);
float distance3 = Vector3.Distance (c, transform.position);
float distance4 = Vector3.Distance (d, transform.position);
float dis = Mathf.Max (Mathf.Max (distance1, distance2), Mathf.Max (distance3, distance4));
if (dis == distance1) {
dest = a;
} else if (dis == distance2) {
dest = b;
} else if (dis == distance3) {
dest = c;
} else if (dis == distance4) {
dest = d;
}
running = true;
}
if (running) {
agent.destination = dest;
agent.speed = 7.0f;
if (Vector3.Distance(dest, transform.position) < 3.0f) {
running = false;
agent.speed = 2.0f;
}
}
} else if (distance > 10.0f && !running) {
agent.speed = 2.0f;
timer += Time.deltaTime;
if (timer >= wanderTimer) {
Vector3 newPos = RandomNavSphere (transform.position, wanderRadius, -1);
agent.SetDestination (newPos);
//animator.SetBool ("Idle", false);
//animator.SetBool ("Walking", true);
timer = 0;
}
} else if (distance > 10.0f && running) {
agent.destination = dest;
agent.speed = 7.0f;
if (Vector3.Distance(dest, transform.position) < 3.0f) {
running = false;
agent.speed = 2.0f;
}
}
}
}
public static Vector3 RandomNavSphere(Vector3 origin, float dist, int layermask) {
Vector3 randDirection = (Random.insideUnitCircle * dist);
randDirection += origin;
NavMeshHit navHit;
NavMesh.SamplePosition (randDirection, out navHit, dist, layermask);
return navHit.position;
}
public void SetAlive(bool alive) {
_alive = alive;
}
}