Last active
February 17, 2025 15:06
-
-
Save dacanizares/5db9c59281a9c9049bf819acce7e29bc to your computer and use it in GitHub Desktop.
Sample AI Controller - GetRandomReachablePointInRadius - UE4
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include "Test/MovementAIController.h" | |
#include "Runtime/Engine/Classes/Kismet/GameplayStatics.h" | |
#include "AI/NavigationSystemBase.h" | |
#include "NavigationSystem.h" | |
void AMovementAIController::BeginPlay() | |
{ | |
Super::BeginPlay(); | |
GoToRandomWaypoint(); | |
} | |
void AMovementAIController::GoToRandomWaypoint() | |
{ | |
FVector Result; | |
if (GetRandomPointInRadius(GetPawn()->GetActorLocation(), 600, Result)) { | |
MoveToLocation(Result); | |
} | |
} | |
bool AMovementAIController::GetRandomPointInRadius(const FVector& Origin, float Radius, FVector& OutResult) | |
{ | |
UNavigationSystemV1* NavSys = FNavigationSystem::GetCurrent<UNavigationSystemV1>(GetWorld()); | |
if (!NavSys) | |
{ | |
return false; | |
} | |
FVector Result; | |
bool bSuccess = NavSys->K2_GetRandomReachablePointInRadius(GetWorld(), Origin, Result, 600); | |
//Out | |
OutResult = Result; | |
return bSuccess; | |
} | |
void AMovementAIController::OnMoveCompleted(FAIRequestID RequestID, const FPathFollowingResult& Result) | |
{ | |
Super::OnMoveCompleted(RequestID, Result); | |
GoToRandomWaypoint(); | |
} |
Thank you so much Dacanizares.
You're welcome!
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
No problem!