-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathEnemyAI.cs
More file actions
155 lines (124 loc) · 4.69 KB
/
Copy pathEnemyAI.cs
File metadata and controls
155 lines (124 loc) · 4.69 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
using UnityEngine;
using System.Collections;
using Pathfinding; //link between script and A* Pathfinding - how to import it
[RequireComponent(typeof(Rigidbody2D))]
[RequireComponent(typeof(Seeker))] //the component that allows the enemy to seek, the scrip from A* pathfinding algorithm
public class EnemyAI : MonoBehaviour {
//What to chase
public Transform target;
//How many times each second we will update our path
public float updateRate = 2f;
//Caching
private Seeker seeker;
private Rigidbody2D rb;
//store the calculated path
public Path path;
//The AI's speed per second
public float speed = 300f;
public ForceMode2D fMode;//force mode = a way to change between force and impluse; how to control the way our force is applied to the rigidbody. Gives us control in the editor as how we want our enemy to behave
[HideInInspector]
public bool pathIsEnded = false;
public float nextWaypointDistance = 3;//how close do we need to get to a waypoint befre it can continue to the next waypoint. max distance from the AI to a waypoint for it to continue to the next waypoint
//The waypoint we are currently moving towards to
private int currentWaypoint = 0;
private bool searchingForPlayer = false;
void Start()
{
seeker = GetComponent<Seeker>(); //asigning seeker
rb = GetComponent<Rigidbody2D>(); //asigning the rigidboy
if (target == null)
{
if (!searchingForPlayer)
{
searchingForPlayer = true;
StartCoroutine(SearchForPlayer());
}
Debug.LogError("No player found! Not good.");
return;
}
//start a new path to the target position, return the result to the OnPathCOmplete method
seeker.StartPath(transform.position, target.position, OnPathComplete);
StartCoroutine(UpdatePath());//so the enemy will update their path when the player moves
}
IEnumerator SearchForPlayer()
{
GameObject sResult = GameObject.FindGameObjectWithTag("Player");
if (sResult == null)
{
yield return new WaitForSeconds(0.5f);
StartCoroutine(SearchForPlayer());
}
else
{
target = sResult.transform;
Debug.Log("Searching for " + target.name);
searchingForPlayer = false;
StartCoroutine(UpdatePath());
yield return false;
}
}
IEnumerator UpdatePath()
{
if (target == null)
{
if (!searchingForPlayer)
{
searchingForPlayer = true;
StartCoroutine(SearchForPlayer());
}
yield return false;
}
seeker.StartPath(transform.position, target.position, OnPathComplete);
yield return new WaitForSeconds(1f / updateRate);
StartCoroutine(UpdatePath());
Debug.Log("Searching for " + target.name);
}
public void OnPathComplete(Path p)
{
Debug.Log("We got a path. DId it have an error?" + p.error);
if (!p.error)
{
path = p;
currentWaypoint = 0;
}
}
void FixedUpdate()
{
if (target == null)
{
if (!searchingForPlayer)
{
searchingForPlayer = true;
StartCoroutine(SearchForPlayer());
}
return;
}
if (path == null)
{
return ;
}
if (currentWaypoint >= path.vectorPath.Count)
//if we reached our final waypoint vector path returns all the waypoints. we are checkick if current way point is bigger than the ammount of waypoints in the list(Count). if it is it means we reached the end.
{
if (pathIsEnded)
{
return;
}
Debug.Log("End of path reached.");
pathIsEnded = true;
return;
}
pathIsEnded = false;
//Direction to the next waypoint.vectorpath - list of waypoints
Vector3 dir = ( path.vectorPath[currentWaypoint] - transform.position).normalized;
dir *= speed * Time.fixedDeltaTime; //speed bound to update steps
//Moving the AI
rb.AddForce(dir, fMode);
float dist = Vector3.Distance(transform.position, path.vectorPath[currentWaypoint]);
if (dist < nextWaypointDistance)//if we are close to the next waypoint, and then it will follow it
{
currentWaypoint++;
return;
}
}
}