-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathenemyAllScript.cs
More file actions
225 lines (178 loc) · 5.92 KB
/
enemyAllScript.cs
File metadata and controls
225 lines (178 loc) · 5.92 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
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
using UnityEngine;
using UnityEngine.AI;
public class enemyAllScript : MonoBehaviour
{
// The enemy is an agent
[SerializeField] private NavMeshAgent agent;
[SerializeField] private Transform player;
[SerializeField] private LayerMask whatIsPlayer;
// For patrolling
[SerializeField] private float speed = 5f;
private Vector3 lastPosition;
private float currentSpeed;
private Vector3 randomDirection;
[SerializeField] private float moveDuration = 2f;
private float patrolTimer = 0f;
private Vector3 patrolDirection;
// Checking for sight
private Vector3 directionToPlayer;
private bool isInSight = false;
// For attacking
[SerializeField] private float timeBetweenAttacks;
private bool alreadyAttacked;
// States
[SerializeField] private float sightRange, attackRange;
private bool playerInSightRange, playerInAttackRange;
public float health = 50f;
private float initialHealth;
[SerializeField] private GameObject enemy;
[Header("Attack Settings")]
public float attackDamage = 20f;
[Header("Ranged attack settings")]
[SerializeField] private GameObject projectilePrefab;
[SerializeField] private Transform attackPoint;
[SerializeField] private float projectileSpeed = 1f;
// difficulty adjustments
public float damageMultiplier = 1f;
private float healthMultiplier = 1f;
private void Start()
{
agent = GetComponent<NavMeshAgent>();
player = GameObject.Find("Player").transform;
string difficulty = PlayerPrefs.GetString("GameDifficulty", "NORMAL");
ApplyDifficultySettings(difficulty);
lastPosition = Vector3.zero;
initialHealth = health;
SetDirection();
}
private void ApplyDifficultySettings(string difficulty)
{
switch (difficulty)
{
case "EASY":
healthMultiplier = 0.75f;
damageMultiplier = 0.75f;
break;
case "NORMAL":
healthMultiplier = 1f;
damageMultiplier = 1f;
break;
case "HARD":
healthMultiplier = 1.5f;
damageMultiplier = 1.5f;
break;
default:
break;
}
health *= healthMultiplier;
initialHealth *= health;
}
private void Update()
{
playerInSightRange = Physics.CheckSphere(transform.position, sightRange, whatIsPlayer);
playerInAttackRange = Physics.CheckSphere(transform.position, attackRange, whatIsPlayer);
currentSpeed = Vector3.Distance(transform.position, lastPosition) / Time.deltaTime;
lastPosition = transform.position;
isInSight = PlayerInSight();
if ((!playerInSightRange && !playerInAttackRange) || !isInSight) Patrolling();
if (playerInSightRange && !playerInAttackRange && isInSight) ChasePlayer();
if (playerInSightRange && playerInAttackRange && isInSight) AttackPlayer();
}
private void Patrolling()
{
patrolTimer += Time.deltaTime;
transform.position += patrolDirection * speed * Time.deltaTime;
if (patrolTimer >= moveDuration)
{
SetDirection();
patrolTimer = 0f;
}
if (currentSpeed < 0.5)
{
SetDirection();
}
}
private bool PlayerInSight()
{
directionToPlayer = (player.position - transform.position).normalized;
if (Physics.Raycast(transform.position, directionToPlayer, out RaycastHit hit, sightRange))
{
if (hit.transform == player)
{
return true;
}
else
{
return false;
}
}
else
{
return false;
}
}
private void SetDirection()
{
float randomX = Random.Range(-1f, 1f);
float randomZ = Random.Range(-1f, 1f);
patrolDirection = new Vector3(randomX, 0, randomZ).normalized;
transform.LookAt(transform.position + patrolDirection);
}
private void ChasePlayer()
{
agent.SetDestination(player.position);
}
private void AttackPlayer()
{
agent.SetDestination(transform.position);
transform.LookAt(player);
if (!alreadyAttacked)
{
RangedAttack();
alreadyAttacked = true;
Invoke(nameof(ResetAttack), timeBetweenAttacks);
}
}
private void RangedAttack()
{
if (projectilePrefab != null && attackPoint != null)
{
GameObject projectile = Instantiate(projectilePrefab, attackPoint.position, Quaternion.identity);
// get the projectile damage component and set its reference to this enemy
projectileDamage projectileDamageScript = projectile.GetComponent<projectileDamage>();
if (projectileDamageScript != null)
{
projectileDamageScript.enemyScript = this;
projectileDamageScript.damage = attackDamage; // set base damage
}
Rigidbody projectileRb = projectile.GetComponent<Rigidbody>();
if (projectileRb != null)
{
// calculate direction to player
Vector3 directionToPlayer = (player.position - attackPoint.position).normalized;
// add force
projectileRb.AddForce(directionToPlayer * projectileSpeed, ForceMode.Impulse);
}
}
}
private void ResetAttack()
{
alreadyAttacked = false;
}
public void TakeDamage(float damage)
{
health -= damage;
if (health <= 0f)
{
Die();
}
}
private void Die()
{
enemy.SetActive(false);
}
public void RespawnEnemy()
{
health = initialHealth; // reset the enemy's health
}
}