-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathNavAgentExample.cs
More file actions
149 lines (125 loc) · 4.88 KB
/
NavAgentExample.cs
File metadata and controls
149 lines (125 loc) · 4.88 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
using UnityEngine;
using System.Collections;
// ----------------------------------------------------------
// CLASS : NavAgentExample
// DESC : Behaviour to test Unity's NavMeshAgent
// ----------------------------------------------------------
[RequireComponent(typeof(NavMeshAgent))]
public class NavAgentExample : MonoBehaviour
{
// Inspector Assigned Variable
public AIWaypointNetwork WaypointNetwork = null;
public int CurrentIndex = 0;
public bool HasPath = false;
public bool PathPending = false;
public bool PathStale = false;
public NavMeshPathStatus PathStatus = NavMeshPathStatus.PathInvalid;
public AnimationCurve JumpCurve = new AnimationCurve();
// Private Members
private NavMeshAgent _navAgent = null;
// -----------------------------------------------------
// Name : Start
// Desc : Cache MavMeshAgent and set initial
// destination.
// -----------------------------------------------------
void Start ()
{
// Cache NavMeshAgent Reference
_navAgent = GetComponent<NavMeshAgent>();
// Turn off auto-update
/*_navAgent.updatePosition = false;
_navAgent.updateRotation = false;*/
// If not valid Waypoint Network has been assigned then return
if (WaypointNetwork==null) return;
// Set first waypoint
SetNextDestination ( false );
}
// -----------------------------------------------------
// Name : SetNextDestination
// Desc : Optionally increments the current waypoint
// index and then sets the next destination
// for the agent to head towards.
// -----------------------------------------------------
void SetNextDestination ( bool increment )
{
// If no network return
if (!WaypointNetwork) return;
// Calculatehow much the current waypoint index needs to be incremented
int incStep = increment?1:0;
Transform nextWaypointTransform = null;
// Calculate index of next waypoint factoring in the increment with wrap-around and fetch waypoint
int nextWaypoint = (CurrentIndex+incStep>=WaypointNetwork.Waypoints.Count)?0:CurrentIndex+incStep;
nextWaypointTransform = WaypointNetwork.Waypoints[nextWaypoint];
// Assuming we have a valid waypoint transform
if (nextWaypointTransform!=null)
{
// Update the current waypoint index, assign its position as the NavMeshAgents
// Destination and then return
CurrentIndex = nextWaypoint;
_navAgent.destination = nextWaypointTransform.position;
return;
}
// We did not find a valid waypoint in the list for this iteration
CurrentIndex=nextWaypoint;
}
// ---------------------------------------------------------
// Name : Update
// Desc : Called each frame by Unity
// ---------------------------------------------------------
void Update ()
{
// Copy NavMeshAgents state into inspector visible variables
HasPath = _navAgent.hasPath;
PathPending = _navAgent.pathPending;
PathStale = _navAgent.isPathStale;
PathStatus = _navAgent.pathStatus;
// If agent is on an offmesh link then perform a jump
if (_navAgent.isOnOffMeshLink)
{
StartCoroutine( Jump( 1.0f) );
return;
}
// If we don't have a path and one isn't pending then set the next
// waypoint as the target, otherwise if path is stale regenerate path
if ( ( _navAgent.remainingDistance<=_navAgent.stoppingDistance && !PathPending) || PathStatus==NavMeshPathStatus.PathInvalid /*|| PathStatus==NavMeshPathStatus.PathPartial*/)
{
SetNextDestination ( true );
}
else
if (_navAgent.isPathStale)
SetNextDestination ( false );
}
// ---------------------------------------------------------
// Name : Jump
// Desc : Manual OffMeshLInk traversal using an Animation
// Curve to control agent height.
// ---------------------------------------------------------
IEnumerator Jump ( float duration )
{
// Get the current OffMeshLink data
OffMeshLinkData data = _navAgent.currentOffMeshLinkData;
// Start Position is agent current position
Vector3 startPos = _navAgent.transform.position;
// End position is fetched from OffMeshLink data and adjusted for baseoffset of agent
Vector3 endPos = data.endPos + ( _navAgent.baseOffset * Vector3.up);
// Used to keep track of time
float time = 0.0f;
// Keeo iterating for the passed duration
while ( time<= duration )
{
// Calculate normalized time
float t = time/duration;
// Lerp between start position and end position and adjust height based on evaluation of t on Jump Curve
_navAgent.transform.position = Vector3.Lerp( startPos, endPos, t ) + (JumpCurve.Evaluate(t) * Vector3.up) ;
// Accumulate time and yield each frame
time += Time.deltaTime;
yield return null;
}
// NOTE : Added this for a bit of stability to make sure the
// Agent is EXACTLY on the end position of the off mesh
// link before completeing the link.
_navAgent.transform.position = endPos;
// All done so inform the agent it can resume control
_navAgent.CompleteOffMeshLink();
}
}