-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBasicMover.cs
More file actions
63 lines (46 loc) · 1.54 KB
/
BasicMover.cs
File metadata and controls
63 lines (46 loc) · 1.54 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
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.AI;
[RequireComponent (typeof(NavMeshAgent))]
public class BasicMover : MonoBehaviour
{
NavMeshAgent agent = null;
[SerializeField] Transform[] waypoints = null;
private int currentWP = -1;
const float CLOSE_DIST = 2.0f;
const float GET_NEXT_CD = .25f;
private float cooldown = 0; //So the agent does call GetNextWaypoint a ton of times (via Update)
private void Start()
{
agent = GetComponent<NavMeshAgent>();
if (waypoints.Length == 0)
throw new System.Exception($"{name} needs waypoints!");
currentWP = 0;
}
void Update()
{
if (CloseToCurrentWaypoint) //It will be close for a few frames
MoveToNextWaypoint(); //So this will get called more than once
if (cooldown > 0)
cooldown -= Time.deltaTime;
}
private void MoveToNextWaypoint()
{
if (cooldown > 0) //That's why I put in a cooldown
return;
Vector3 pos = GetNextWaypoint();
agent.SetDestination(pos);
agent.isStopped = false;
cooldown = GET_NEXT_CD;
}
private Vector3 GetNextWaypoint()
{
if (currentWP == waypoints.Length - 1)
currentWP = 0;
else
currentWP++;
return waypoints[currentWP].position;
}
private bool CloseToCurrentWaypoint => agent.remainingDistance <= CLOSE_DIST;
}