/// <summary> /// Called when one of the requested paths is calculated. /// </summary> /// <param name="pointPath">The calculated path consisting of waypoints.</param> /// <param name="facePath">The calculated path consisting of faces.</param> protected override void OnPath(Vector3[] pointPath, IImmutableFace[] facePath) { if (currentFace == null) { Debug.LogError("The agent lost the track of the current cell."); } if (pointPath == null || facePath == null) { OnPathNotFound(); } else { int facesCount = facePath.Length; for (int i = 0; i < facesCount; i++) { if (facePath[i] == currentFace.source) { progress = new PathProgress(1, pointPath, i, facePath); return; } } progress = null; if (!HasPendingRequests) { RequestPath(position, pointPath[pointPath.Length - 1], PathType, currentFace.source); } } }
private void Update() { if (currentFace == null) { return; } Vector3 acceleration; if (progress == null) { if (velocity != Vector3.zero) { acceleration = Vector3.ClampMagnitude(-velocity, maxAcceleration); } else { acceleration = Vector3.zero; } } else { if (progress.IsFinished) { progress = null; if (!HasPendingRequests) { RaiseOnDestinationReached(); } Update(); return; } if (Vector3.Distance(position, progress.CurrentPoint) < stoppingDistance) { progress.currentPoint++; Update(); return; } acceleration = CalculateAccelerationToSeek(position, progress.CurrentPoint, !progress.HasNextPoint); } acceleration = ApplyModifiers(acceleration); velocity += acceleration * Time.deltaTime; Debug.DrawRay(transform.position, velocity); if (velocity.sqrMagnitude > .0001f) { velocity = Vector3.ClampMagnitude(velocity, maxVelocity); Move(velocity * Time.deltaTime); ApplyPosition(); transform.forward = Vector3.MoveTowards(transform.forward, new Vector3(velocity.x, 0, velocity.z), rotationSpeed * Time.deltaTime); } else { velocity = Vector3.zero; } }
/// <summary> /// Called when requests are canceled. /// </summary> protected override void OnRequestsCanceled() { progress = null; }
private void OnPathNotFound() { Debug.Log("No path found."); progress = null; }