Game Development Reference
In-Depth Information
22 //Set destination
23 Agent.SetDestination(hit.position);
24
25 //Set distance range between object and destination to classify as 'arrived'
26 float ArrivalDistance = 2.0f;
27
28 //Set timeout before new path is generated (5 seconds)
29 float TimeOut = 5.0f;
30
31 //Elapsed Time
32 float ElapsedTime = 0;
33
34 //Wait until enemy reaches destination or times-out, and then get new position
35 while(Vector3.Distance(ThisTransform.position, hit.position) >
ArrivalDistance && ElapsedTime < TimeOut)
36 {
37 //Update ElapsedTime
38 ElapsedTime += Time.deltaTime;
39
40 //Check if should enter chase state
41 if(Vector3.Distance(ThisTransform.position, PlayerTransform.position) <
ChaseDistance)
42 {
43 //Exit patrol and enter chase state
44 //ChangeState(ENEMY_STATE.CHASE);
45 //yield break;
46 }
47
48 yield return null;
49 }
50 }
51 }
52 //------------------------------------------------
Line 07 . This coroutine begins by calling the Stop function in the member agent.
The member agent is newly added to the class as a protected NavMeshAgent
agent. It is a reference to the NavMeshAgent component attached to the game
object. An instance to this component is retrieved in the Start event for the
class. The Stop function of NavMeshAgent simply terminates any outstanding
navigation and movement operations, if they were any.
Note More information on the NavMeshAgent component can be found at the online Unity documentation at
https://docs.unity3d.com/Documentation/ScriptReference/NavMeshAgent.html .
 
Search WWH ::




Custom Search