Ai. Find the nearest object

Hello everyone,

How to get the length/cost of the path to the object?

I want to find the nearest object, not in a straight line but around obstacles.

But for some reason the first ball (lenght = 1300) is closer than the second (2600), though to reach it you need to get around the wall.

PS: and what is the difference of cost and length ?

And NavSys->GetPathLength and NavSys->FindPathToLocationSynchronously(…)->GetPathLength() ?

Thanks!

I try to do so:

UNavigationSystem* NavSys = UNavigationSystem::GetCurrent(GetWorld());

NavSys->GetPathLength(Pawn->GetActorLocation(), FVector(380, -650, 100), PathLength);
GEngine->AddOnScreenDebugMessage(-1, 5.f, FColor::Yellow, FString::SanitizeFloat(PathLength));
    
NavSys->GetPathLength(Pawn->GetActorLocation(), FVector(1020, 760, 100), PathLength);
GEngine->AddOnScreenDebugMessage(-1, 5.f, FColor::Yellow, FString::SanitizeFloat(PathLength));

or

NavSys->FindPathToLocationSynchronously(GetWorld(), Pawn->GetActorLocation(), FVector(380, -650, 100))->GetPathLength();
NavSys->FindPathToLocationSynchronously(GetWorld(), Pawn->GetActorLocation(), FVector(1020, 760, 100))->GetPathLength();

Path cost is the cost that the path has. Each navigation will have a “Area Class” associated with it, in the “Area Class” you can define the cost it takes to travel over this area, this is simply multiplied with the path length. It also has an enter cost, meaning that it costs X to enter that navigation area and you can modify it’s draw color. Cost * length + enter cost = path cost.

The Path length is simply the length between the path points from the actor location towards the end goal.

You can use the “Navigation Testing Actor” to see the path cost and length.

If you’re going to keep bumping your question, you should at least respond to the comment made, or add some more info. This is not really a question that is easily answered - either you are doing something wrong, or there is a bug, but you haven’t provided full code or full output so it’s hard to say.

I’d start by making your testing code more explicit: check that GetPathLength() is returning Success, then print out the name/location of the path target along with the length so you’re certain about the results you’re seeing. If there is still an issue, do what Tekoppar says and use the navigation testing actor. You need to drop two of them into your level and set the SearchStart property for one of them to true. You can then move them around and it will show you the path found between them, with more info in the details panel when you select the starting actor.

Strange, I created a project from scratch, and it worked. Whatever it was thanks for the help, I did not know about “Navigation Testing Actor”, he helped me in debugging.