-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathEnemyAI.cs
More file actions
110 lines (88 loc) · 3.31 KB
/
EnemyAI.cs
File metadata and controls
110 lines (88 loc) · 3.31 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
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.AI;
public class EnemyAI : MonoBehaviour
{
// Projectile (must be a GameObject with Rigidbody)
public GameObject Projectile;
public float ProjectileSpeed = 40f;
public float despawn = 0.2f; // How long until the projectile automatically disappears
// Combat info
public float Health = 10;
public Transform Barrel; // Or whatever the projectile shoots out of
// Navigation info
[SerializeField] private NavMeshAgent agent; // Pathfinding
public Transform player; // The player to target
public LayerMask Ground, Player;
// Patrol
private Vector3 walkPoint;
public float walkPointRange = 5f;
private bool walkPointSet;
// Attack
public float timeBetweenAttacks = 1f;
private bool attacked;
public float sightRange, attackRange;
private bool playerInSightRange, playerInAttackrange;
private void Awake()
{
agent = GetComponent<NavMeshAgent>();
}
private void FixedUpdate()
{
// Check for the player within a radius and act accordingly
playerInSightRange = Physics.CheckSphere(transform.position, sightRange, Player);
playerInAttackrange = Physics.CheckSphere(transform.position, attackRange, Player);
if (!playerInSightRange && !playerInAttackrange) Patrol();
if (playerInSightRange && !playerInAttackrange) Chase();
if (playerInSightRange && playerInAttackrange) Attack();
}
private void Patrol()
{
if (!walkPointSet) FindWalkPoint();
if (walkPointSet) agent.SetDestination(walkPoint);
Vector3 distanceToWalkPoint = transform.position - walkPoint;
// If the random position is too close, retry
if (distanceToWalkPoint.magnitude < 1f)
walkPointSet = false;
}
private void FindWalkPoint()
{
float z = Random.Range(-walkPointRange, walkPointRange);
float x = Random.Range(-walkPointRange, walkPointRange);
walkPoint = new Vector3(transform.position.x + x, transform.position.y, transform.position.z + z);
// Check that the walk point is on solid ground
if (Physics.Raycast(walkPoint, -transform.up, 2f, Ground))
walkPointSet = true;
}
private void Chase()
{
agent.SetDestination(player.position);
}
private void Attack()
{
agent.SetDestination(transform.position); // Halt the enemy
transform.LookAt(player);
if (!attacked)
{
GameObject spawnProjectile = Instantiate(Projectile, Barrel.position, Barrel.rotation);
spawnProjectile.GetComponent<Rigidbody>().velocity = ProjectileSpeed * transform.forward;
attacked = true;
Invoke(nameof(ResetAttack), timeBetweenAttacks); // Calls function ResetAttack() after delay
Destroy(spawnProjectile, despawn);
}
}
private void ResetAttack()
{
attacked = false;
}
public void TakeDamage(float damage)
{
Health -= damage;
if (Health <= 0) Invoke(nameof(DestroyEnemy), 0.5f);
}
private void DestroyEnemy()
{
Destroy(gameObject);
}
}