Isn’t updating timers in every update cycle inefficient? Wouldn’t using something like the code below this cut down on the amount of updates?
Although we are only dealing with a couple of AI controlled characters at the moment and it wouldn’t have much effect - the moment you tried to scale up the project for thousands of AI characters it would.
private void UpdateTimers()
{
_timeSinceLastSawPlayer += Time.deltaTime;
// _timeSinceArrivedAtWaypoint += Time.deltaTime;
}
private void PatrolBehaviour()
{
Vector3 nextPosition = _guardPosition;
if (patrolPath != null)
{
if (AtWaypoint())
{
_timeSinceArrivedAtWaypoint = Time.time;
// _timeSinceArrivedAtWaypoint = 0;
CycleWaypoint();
}
nextPosition = GetCurrentWaypoint();
}
if (_timeSinceArrivedAtWaypoint + waypointDwellTime < Time.time)
// if (_timeSinceArrivedAtWaypoint > waypointDwellTime)
{
mover.StartMoveAction(nextPosition);
}
}
If you find there is a pause before the Guard goes to the first point set the initial value to negative infinity.
private float _timeSinceArrivedAtWaypoint = -Mathf.Infinity;