void UpdateDestination(Vector3 destination) { Vector3 delta = MoveDestination - destination; if (delta.Length() > 0.01f) // Only recalculate path when the target position is different { // Generate a new path using the navigation component pathToDestination.Clear(); if (Navigation.TryFindPath(destination, pathToDestination)) { // Skip the points that are too close to the enemy WaypointIndex = 0; while (!ReachedDestination && (CurrentWaypoint - Entity.Transform.WorldMatrix.TranslationVector).Length() < 0.25f) { WaypointIndex++; } // If this path still contains more points, set the enemy to running if (!ReachedDestination) { IsRunning = true; MoveDestination = destination; } } else { // Could not find a path to the target location pathToDestination.Clear(); HaltMovement(); } } }
private void UpdateDestination(Vector3 destination) { Vector3 delta = moveDestination - destination; if (delta.Length() > 0.01f) // Only recalculate path when the target position is different { pathToDestination.Clear(); if (navigation.TryFindPath(destination, pathToDestination)) { // Skip the points that are too close to the player waypointIndex = 0; while (!ReachedDestination && (CurrentWaypoint - Entity.Transform.WorldMatrix.TranslationVector).Length() < 0.25f) { waypointIndex++; } if (!ReachedDestination) { isRunning = true; moveDestination = destination; } } else { // Could not find a path to the target location pathToDestination.Clear(); HaltMovement(); } } }
/// <summary> /// Tries to move towards the target position using the attached NavigationComponent asynchronously /// </summary> /// <param name="targetPosition"></param> /// <returns></returns> protected IEnumerator <Vector3> Move(Vector3 targetPosition) { NavigationComponent navigationComponent = Entity.Get <NavigationComponent>(); List <Vector3> pathPoints = new List <Vector3>(); if (navigationComponent == null || navigationComponent.NavigationMesh == null) { pathPoints = new List <Vector3> { Entity.Transform.WorldMatrix.TranslationVector, targetPosition }; } else { if (!navigationComponent.TryFindPath(targetPosition, pathPoints)) { yield break; } } Path navigationPath = new Path(pathPoints.ToArray()); Waypoint nextWaypoint = navigationPath.Waypoints[0]; while (nextWaypoint != null) { Vector3 targetSpeed = Vector3.Zero; if (!Drone.Stunned) { // Move towards target when having a waypoint Vector3 dir = nextWaypoint.Position - Entity.Transform.WorldMatrix.TranslationVector; dir.Y = 0; var dist = dir.Length(); if (dist < MoveThreshold) { nextWaypoint = nextWaypoint.Next; continue; } dir.Normalize(); Drone.UpdateBodyRotation(dir); targetSpeed = dir * Drone.MaximumSpeed; float dt = (float)Game.UpdateTime.Elapsed.TotalSeconds; var estimatedDist = targetSpeed.Length() * dt; if (estimatedDist > dist) { targetSpeed = dir * (dist / dt); } } Drone.SetMovement(targetSpeed); yield return(nextWaypoint.Position); } // Stop when done moving Drone.SetMovement(Vector3.Zero); }