Hello everyone, I’m having a bit of trouble at implementing my own movement system for a simple RTS game.
As I’d like to obtain large amount of actors controlled by AI/Player, using the ACharacter class I get awful performances due to the high (and unecessary for my purposes) complexity. So I decided to go with a custom character class with only the functionalities I really need.
I’m implementing a SearchPath function which takes as input the destination vector and a reference to the output array of path points.
Overall the code works, as it outputs a reasonable set of points to be followed.
However, I cannot figure out:
- Why the FindPathSync() method does not seem to take into consideration the ActorHeight and ActorRadius I set in the FNavAgentProperties. (i.e. I always get points that do not have the Z coordinate adjusted depending on the mentioned parameters).
- Why am I getting all points to be +10 in Z (e.g. If my destination point is (x,y,z) I get the last point to be (x,y,z+10).
Here’s the mentioned code:
bool AStrategyAIController::SearchPath(const FVector& Destination, TArray<FVector> & OutPathPoints) const
{
// Build query for navigation system
FPathFindingQuery Query;
FAIMoveRequest MoveRequest;
MoveRequest.SetAcceptanceRadius(3.f);
MoveRequest.SetGoalLocation(Destination);
const bool bValidQuery = BuildPathfindingQuery(MoveRequest, Query);
// Get navigation system and initialize result/agent properties
UWorld* World = GetWorld();
if (World == nullptr) return false;
UNavigationSystemV1* NavSys = FNavigationSystem::GetCurrent<UNavigationSystemV1>(GetWorld());
FNavAgentProperties navAgentProperties;
navAgentProperties.AgentHeight = SelectablePawn->GetCapsuleComponent()->GetScaledCapsuleHalfHeight() * 2;
navAgentProperties.AgentRadius = SelectablePawn->GetCapsuleComponent()->GetScaledCapsuleRadius();
FPathFindingResult PathFindingResult;
// Calculate path synchronously and populate array of points
if (NavSys != nullptr)
{
PathFindingResult = NavSys->FindPathSync(navAgentProperties, Query);
if (PathFindingResult.Result != ENavigationQueryResult::Error)
{
if (PathFindingResult.IsSuccessful() && PathFindingResult.Path.IsValid())
{
for(FNavPathPoint point : PathFindingResult.Path->GetPathPoints())
{
OutPathPoints.Add(point.Location);
}
}
}
}
return true;
}
I’m surely missing something here, therefore I kindly ask for your help.