-
Notifications
You must be signed in to change notification settings - Fork 3
/
Enemy.cs
167 lines (142 loc) · 4.16 KB
/
Enemy.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
using UnityEngine;
using System.Collections;
using UnityEngine.AI;
public class Enemy : MonoBehaviour
{
public NavMeshAgent navAgent;
public Transform player;
public LayerMask groundLayer, playerLayer;
public float health;
public float walkPointRange;
public float timeBetweenAttacks;
public float sightRange;
public float attackRange;
public int damage;
public Animator animator;
public ParticleSystem hitEffect;
private Vector3 walkPoint;
private bool walkPointSet;
private bool alreadyAttacked;
private bool takeDamage;
private void Awake()
{
animator = GetComponent<Animator>();
player = GameObject.Find("Player").transform;
navAgent = GetComponent<NavMeshAgent>();
}
private void Update()
{
bool playerInSightRange = Physics.CheckSphere(transform.position, sightRange, playerLayer);
bool playerInAttackRange = Physics.CheckSphere(transform.position, attackRange, playerLayer);
if (!playerInSightRange && !playerInAttackRange)
{
Patroling();
}
else if (playerInSightRange && !playerInAttackRange)
{
ChasePlayer();
}
else if (playerInAttackRange && playerInSightRange)
{
AttackPlayer();
}
else if (!playerInSightRange && takeDamage)
{
ChasePlayer();
}
}
private void Patroling()
{
if (!walkPointSet)
{
SearchWalkPoint();
}
if (walkPointSet)
{
navAgent.SetDestination(walkPoint);
}
Vector3 distanceToWalkPoint = transform.position - walkPoint;
animator.SetFloat("Velocity", 0.2f);
if (distanceToWalkPoint.magnitude < 1f)
{
walkPointSet = false;
}
}
private void SearchWalkPoint()
{
float randomZ = Random.Range(-walkPointRange, walkPointRange);
float randomX = Random.Range(-walkPointRange, walkPointRange);
walkPoint = new Vector3(transform.position.x + randomX, transform.position.y, transform.position.z + randomZ);
if (Physics.Raycast(walkPoint, -transform.up, 2f, groundLayer))
{
walkPointSet = true;
}
}
private void ChasePlayer()
{
navAgent.SetDestination(player.position);
animator.SetFloat("Velocity", 0.6f);
navAgent.isStopped = false; // Add this line
}
private void AttackPlayer()
{
navAgent.SetDestination(transform.position);
if (!alreadyAttacked)
{
transform.LookAt(player.position);
alreadyAttacked = true;
animator.SetBool("Attack", true);
Invoke(nameof(ResetAttack), timeBetweenAttacks);
RaycastHit hit;
if (Physics.Raycast(transform.position, transform.forward, out hit, attackRange))
{
/*
YOU CAN USE THIS TO GET THE PLAYER HUD AND CALL THE TAKE DAMAGE FUNCTION
PlayerHUD playerHUD = hit.transform.GetComponent<PlayerHUD>();
if (playerHUD != null)
{
playerHUD.takeDamage(damage);
}
*/
}
}
}
private void ResetAttack()
{
alreadyAttacked = false;
animator.SetBool("Attack", false);
}
public void TakeDamage(float damage)
{
health -= damage;
hitEffect.Play();
StartCoroutine(TakeDamageCoroutine());
if (health <= 0)
{
Invoke(nameof(DestroyEnemy), 0.5f);
}
}
private IEnumerator TakeDamageCoroutine()
{
takeDamage = true;
yield return new WaitForSeconds(2f);
takeDamage = false;
}
private void DestroyEnemy()
{
StartCoroutine(DestroyEnemyCoroutine());
}
private IEnumerator DestroyEnemyCoroutine()
{
animator.SetBool("Dead", true);
yield return new WaitForSeconds(1.8f);
Destroy(gameObject);
}
private void OnDrawGizmosSelected()
{
Gizmos.color = Color.red;
Gizmos.DrawWireSphere(transform.position, attackRange);
Gizmos.color = Color.yellow;
Gizmos.DrawWireSphere(transform.position, sightRange);
}
}