Vector3 GetValidMovePosition(Vector3 currentTarget) { var dir = (currentTarget - myHero.Position); dir.Normalize(); // try to walk further if position is not valid foreach (var possibleTarget in GetPositionsWithDistanceAndAngle(currentTarget, dir, pathfinding.CellSize, 4)) { var flags = pathfinding.GetCellFlags(possibleTarget); if (IsWalkableCell(flags)) { return(possibleTarget); } } // no valid position found for our old movement target, so try to find a new one foreach (var possibleTarget in GetPositionsWithDistanceAndAngle(GetValidMovePosition(), dir, pathfinding.CellSize, 4)) { var flags = pathfinding.GetCellFlags(possibleTarget); if (IsWalkableCell(flags)) { return(possibleTarget); } } // well no luck? Debugging.WriteLine("Couldn't find a valid target position which is safe :("); return(GetValidMovePosition()); }
/// <summary> /// The get valid move position. /// </summary> /// <param name="currentTarget"> /// The current target. /// </param> /// <param name="unit"> /// The unit. /// </param> /// <param name="pathfinding"> /// The pathfinding. /// </param> /// <returns> /// The <see cref="Vector3" />. /// </returns> public static Vector3 GetValidMovePosition( Vector3 currentTarget, IAbilityUnit unit, NavMeshPathfinding pathfinding) { var dir = currentTarget - unit.SourceUnit.Position; dir.Normalize(); // try to walk further if position is not valid foreach (var possibleTarget in GetPositionsWithDistanceAndAngle(currentTarget, dir, pathfinding.CellSize, 4) ) { var flags = pathfinding.GetCellFlags(possibleTarget); if (IsWalkableCell(flags)) { return(possibleTarget); } } // var perpendicular = dir.Perpendicular(); // perpendicular.Normalize(); // var newTarget = unit.SourceUnit.Position + (perpendicular * 50); // foreach (var possibleTarget in GetPositionsWithDistanceAndAngle(newTarget, perpendicular, pathfinding.CellSize, 4)) // { // var flags = pathfinding.GetCellFlags(possibleTarget); // if (IsWalkableCell(flags)) // { // return possibleTarget; // } // } // newTarget = unit.SourceUnit.Position + (perpendicular * -50); // foreach (var possibleTarget in GetPositionsWithDistanceAndAngle(newTarget, perpendicular, pathfinding.CellSize, 4)) // { // var flags = pathfinding.GetCellFlags(possibleTarget); // if (IsWalkableCell(flags)) // { // return possibleTarget; // } // } var newTarget = unit.SourceUnit.Position + dir * -50; // no valid position found for our old movement target, so try to find a new one foreach (var possibleTarget in GetPositionsWithDistanceAndAngle(newTarget, dir, pathfinding.CellSize, 4)) { var flags = pathfinding.GetCellFlags(possibleTarget); if (IsWalkableCell(flags)) { return(possibleTarget); } } // well no luck? return(newTarget); }
public static Vector3 ExtendUntilWall( Vector3 start, Vector3 direction, float distance, NavMeshPathfinding pathFinder) { var step = pathFinder.CellSize / 2f; var testPoint = start; var sign = distance > 0f ? 1f : -1f; distance = Math.Abs(distance); while (pathFinder.GetCellFlags(testPoint).HasFlag(NavMeshCellFlags.Walkable) && distance > 0f) { if (step > distance) { step = distance; } testPoint = testPoint + sign * direction * step; distance -= step; } return(testPoint); }