-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathEnemyAI.cs
More file actions
59 lines (51 loc) · 1.39 KB
/
Copy pathEnemyAI.cs
File metadata and controls
59 lines (51 loc) · 1.39 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
using UnityEngine;
using System.Collections;
public class EnemyAI : MonoBehaviour
{
public Transform[] Waypoints;
public float Speed;
public int curWayPoint;
public bool doPatrol = true;
public Vector3 Target;
public Vector3 MoveDirection;
public Vector3 Velocity;
private UnityEngine.AI.NavMeshAgent agent;
void OnTriggerEnter(){
agent.SetDestination (Target);
}
void Update()
{
agent = GetComponent<UnityEngine.AI.NavMeshAgent>();
agent.SetDestination (Target);
{
agent.SetDestination (Target);
}
if (curWayPoint < Waypoints.Length)
{
Target = Waypoints[curWayPoint].position;
MoveDirection = Target - transform.position;
Velocity = GetComponent<Rigidbody>().velocity;
if(MoveDirection.magnitude < 1)
{
curWayPoint++;
}
else
{
Velocity = MoveDirection.normalized * Speed;
}
}
else
{
if(doPatrol)
{
curWayPoint=0;
}
else
{
Velocity = Vector3.zero;
}
}
GetComponent<Rigidbody>().velocity = Velocity;
transform.LookAt (Target);
}
}