-
Notifications
You must be signed in to change notification settings - Fork 0
/
EnemyCtrl.cs
311 lines (277 loc) · 10.9 KB
/
EnemyCtrl.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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.AI;
public class EnemyCtrl : MonoBehaviour {
NavMeshAgent agent;
Transform moveTarget;
Animator animator;
public Transform firstPoint;
public GameObject bossNameUI;
public GameObject bossLifeUI_b;
public GameObject bossLifeUI_f;
public float attackDistance;
public bool isChaseAttack;
public float dis = 30;
public float runSpeed = 10;
EnemyaAttackArea enemyaAttackArea;
GameObject player;
Vector3 preEnemyPos;
float time = 0;
bool isIdle;
bool isAttack = false;
//bool AttackPosition = false;
bool chaseKey = false;
//bool attackKey = false;
int attackStatus = 0;
float stopJudgeDis = 0.01f;
bool isMove = true;
Vector3 preAttackPos;
float time2;
enum State{
idle,
walk,
chase,
attack,
die
}
State state;
State nextState;
// Use this for initialization
void Start () {
agent = GetComponent<NavMeshAgent>();
moveTarget = firstPoint;
agent.SetDestination(moveTarget.position);
//agent.destination = moveTarget.position;
animator = GetComponent<Animator>();
state = State.walk;
nextState = State.walk;
enemyaAttackArea = GetComponentInChildren<EnemyaAttackArea>();
player = GameObject.FindWithTag("Player");
preEnemyPos = new Vector3(transform.position.x-5.0f, 0, transform.position.z);
}
// Update is called once per frame
void Update () {
if (!agent.isOnNavMesh) {
Destroy(firstPoint.gameObject);
Destroy(gameObject);
Instantiate((Resources.Load("ErrorAlert") as GameObject), transform.position, Quaternion.identity);
}
if (state == State.walk) { Walk(); }
else if (state == State.idle) { Idle(); }
else if (state == State.chase) { Chase(); }
else if (state == State.attack) { Attack(); }
else { Die(); }
if (nextState != state)
{
if (nextState == State.walk) { state = nextState; }
else if (nextState == State.idle) { state = nextState; }
else if (nextState == State.chase) { state = nextState; }
else if (nextState == State.attack) { state = nextState; preAttackPos = transform.position; }
else { Die(); }
}
//敵がその場で停止することなく、どこかへは向かうようにする。バグ対策
agent.SetDestination(new Vector3(agent.destination.x, transform.position.y, agent.destination.z));
if (Input.GetKeyDown(KeyCode.P)) { StopEnemy(); }
if (Input.GetKeyDown(KeyCode.L)) { RestartEnemy(); }
}
void Walk(){
Vector3 enemyPos = new Vector3(transform.position.x, 0, transform.position.z);
//print(Vector3.Distance(enemyPos, preEnemyPos));
//Vector3 moveTargetPos = new Vector3(moveTarget.position.x, 0, moveTarget.position.z);
//Debug.Log(Vector3.Distance(moveTarget.position, enemyPos));
if(Vector3.Distance(moveTarget.position, enemyPos)<=1.0f || (((Vector3.Distance(enemyPos, preEnemyPos))<stopJudgeDis)&&time>1.0f)){
//Debug.Log("walk1\n");
time = 0;
moveTarget.position = new Vector3(Random.Range(transform.position.x-10.0f, transform.position.x+10.0f), 0, Random.Range(transform.position.z-10.0f, transform.position.z+10.0f));
nextState = State.idle;
animator.SetBool("Idle", true);
}
else{
//Debug.Log("walk2\n");
agent.SetDestination(new Vector3(moveTarget.position.x, transform.position.y, moveTarget.position.z));
transform.LookAt(new Vector3(moveTarget.position.x, transform.position.y, moveTarget.position.z));
}
preEnemyPos = new Vector3(transform.position.x, 0, transform.position.z);
time += Time.deltaTime;
}
void Idle(){
return;
}
void Chase()
{
if (!isChaseAttack) { Attack(); return; }
animator.SetBool("Idle", false);
//攻撃が当たってないならplayerを追い続ける
if (!chaseKey && Vector3.Distance(transform.position, new Vector3(player.transform.position.x, transform.position.y, player.transform.position.z)) > 10.0f)
{
//突進開始
enemyaAttackArea.OnAttack();
//moveTarget.position = new Vector3(player.transform.position.x, transform.position.y, player.transform.position.z);
agent.SetDestination(new Vector3(player.transform.position.x, transform.position.y, player.transform.position.z));
transform.LookAt(new Vector3(player.transform.position.x, transform.position.y, player.transform.position.z));
}
else if (!chaseKey){
//攻撃が当たったらそのまま走る
//enemyaAttackArea.OnAttackTermination();
moveTarget.position = transform.position + transform.forward * dis ;
agent.SetDestination(moveTarget.position);
transform.LookAt(moveTarget.position);
chaseKey = !chaseKey;
}
else{
Vector3 destination = new Vector3(agent.destination.x, transform.position.y, agent.destination.z);
if(Vector3.Distance(transform.position, destination)<=1.0f){
chaseKey = false;
enemyaAttackArea.isHit = false;
int j = Random.Range(0, 3);
if(j==0){nextState = State.chase;}
else{ nextState = State.attack; }
}
else
{
agent.SetDestination(moveTarget.position);
transform.LookAt(moveTarget.position);
if (!enemyaAttackArea.isHit) enemyaAttackArea.OnAttack();
else { enemyaAttackArea.OnAttackTermination(); }
}
}
}
void Attack(){
animator.SetBool("Idle", false);
if (attackStatus == 2 && (Vector3.Distance(transform.position, preAttackPos) < 0.1f))
{
time2 += Time.deltaTime;
if (time2 >= 5.0f) { Destroy(gameObject); Instantiate((Resources.Load("ErrorAlert") as GameObject), transform.position, Quaternion.identity); }
}
else { time2 = 0; }
preAttackPos = transform.position;
if(attackStatus==0){ //playerと距離があるなら近づく
agent.SetDestination(new Vector3(player.transform.position.x, transform.position.y, player.transform.position.z));
transform.LookAt(new Vector3(player.transform.position.x, transform.position.y, player.transform.position.z));
Vector3 destination = new Vector3(player.transform.position.x, transform.position.y, player.transform.position.z);
if (Vector3.Distance(transform.position, destination) <= attackDistance) //playerの近くにきたら
{
//AttackPosition = true;
attackStatus = 1;
}
}
else if(attackStatus==1){ //攻撃中
agent.isStopped = true;
animator.SetBool("attack", true);
moveTarget.position = transform.position + transform.forward * dis + transform.right * 5;
}
else if(attackStatus==2){
Vector3 destination = new Vector3(moveTarget.position.x, transform.position.y, moveTarget.position.z);
if (Vector3.Distance(transform.position, destination) <= 1.0f)
{
//AttackPosition = false;
//attackKey = false;
attackStatus = 0;
enemyaAttackArea.isHit = false;
int j = Random.Range(0, 2);
if (j == 0) { nextState = State.chase; }
else { nextState = State.attack; }
}
else{
agent.isStopped = false;
agent.SetDestination(moveTarget.position);
transform.LookAt(moveTarget.position);
}
}
}
void Die(){
}
//以下、イベント関数
public void StartIdleAnim()
{
isIdle = true;
}
public void EndIdleAnim(){
isIdle = false;
animator.SetBool("Idle", false);
nextState = State.walk;
}
public void StartRun(){
agent.speed = runSpeed;
}
public void EndRun()
{
enemyaAttackArea.OnAttackTermination();
enemyaAttackArea.hitEffect.SetActive(false);
}
public void StartAttack(){
//Debug.Log("startAttack");
enemyaAttackArea.OnAttack();
transform.LookAt(new Vector3(player.transform.position.x, transform.position.y, player.transform.position.z));
}
public void EndAttack()
{
//Debug.Log("EndAttack");
enemyaAttackArea.OnAttackTermination();
enemyaAttackArea.hitEffect.SetActive(false);
}
public void EndAttack2(){
//Debug.Log("EndAttack2");
//attackKey = true;
attackStatus = 2;
agent.SetDestination(transform.position + transform.forward * dis + transform.right * 5);
transform.LookAt(transform.position + transform.forward * dis + transform.right * 5);
animator.SetBool("attack", false);
}
private void OnTriggerEnter(Collider other) //Playerを探す
{
if(other.gameObject.layer==LayerMask.NameToLayer("Player") && !isAttack){
//Debug.Log("Found Player!");
isAttack = true;
nextState = State.attack;
animator.SetBool("chase", true);
animator.SetBool("Attacking", true);
if(gameObject.tag=="Boss"){
bossNameUI.SetActive(true);
bossLifeUI_b.SetActive(true);
bossLifeUI_f.SetActive(true);
}
}
}
private void OnTriggerExit(Collider other)
{
if (other.gameObject.layer == LayerMask.NameToLayer("Player") && isAttack)
{
//Debug.Log("Lost Player!");
isAttack = false;
chaseKey = false;
//AttackPosition = false;
//attackKey = false;
attackStatus = 0;
enemyaAttackArea.isHit = false;
nextState = State.walk;
animator.SetBool("chase", false);
animator.SetBool("Attacking", false);
moveTarget.position = new Vector3(firstPoint.transform.position.x, 0, firstPoint.transform.position.z);
if (gameObject.tag == "Boss")
{
bossNameUI.SetActive(false);
bossLifeUI_b.SetActive(false);
bossLifeUI_f.SetActive(false);
}
}
}
public void StopEnemy(){
if (isMove)
{
animator.StartPlayback();
agent.Stop();
isMove = false;
}
}
public void RestartEnemy()
{
if (!isMove)
{
animator.StopPlayback();
agent.Resume();
isMove = true;
}
}
}