Guest User

Untitled

a guest
Jul 7th, 2021
41
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. UFUNCTION(BlueprintCallable, meta = (WorldContext = "WorldContextObject"))
  2. static UNavigationPath* FindPathToLocationWithOffset(UObject* WorldContextObject, const FVector& PathStart, const FVector& PathEnd, const float OffsetDistance, AActor* PathfindingContext = nullptr, TSubclassOf<UNavigationQueryFilter> FilterClass = nullptr);
  3.  
  4. UNavigationPath* URTSTools::FindPathToLocationWithOffset(UObject* WorldContextObject, const FVector& PathStart, const FVector& PathEnd, const float OffsetDistance, AActor* PathfindingContext, TSubclassOf<UNavigationQueryFilter> FilterClass)
  5. {
  6.     UNavigationSystemV1* NavSys = UNavigationSystemV1::GetCurrent(WorldContextObject);
  7.  
  8.     UNavigationPath* NavPath = NavSys->FindPathToLocationSynchronously(WorldContextObject, PathStart, PathEnd, PathfindingContext, FilterClass);
  9.  
  10.     // Return invalid path so we can handle invalidity in blueprint
  11.     if (!NavPath->IsValid())
  12.     {
  13.         return NavPath;
  14.     }
  15.  
  16.     // Offset path points from corners only if distance is valid
  17.     if (OffsetDistance > 0)
  18.     {
  19.         const FNavPathSharedPtr NavPathPtr = NavPath->GetPath();
  20.        
  21.         NavPathPtr.Get()->CastPath<FNavMeshPath>()->OffsetFromCorners(OffsetDistance);
  22.  
  23.         UNavigationPath* OffsetPath = NewObject<UNavigationPath>(NavSys);
  24.         OffsetPath->SetPath(NavPathPtr);
  25.        
  26.         // Filter out duplicate points as OffsetFromCorners tends to create doubles
  27.         TArray<FVector> FilteredPoints;
  28.         for (auto Point : OffsetPath->PathPoints)
  29.         {
  30.             FilteredPoints.AddUnique(Point);
  31.         }
  32.  
  33.         OffsetPath->PathPoints = FilteredPoints;
  34.  
  35.         return OffsetPath;
  36.     }
  37.     else
  38.     {
  39.         return NavPath;
  40.     }
  41. }
RAW Paste Data