Why does clicking outside of NavMesh bounds in a click-to-move project make player pawn attempt to move towards world origin?

So, i had similar problems, since i am doing a Click&Move Game, and whenever i clicked outside of navmesh, nothing happened. Though i wanted in that case, that player just moves in direction of click. I solved this by doing some of navigation queries “manually”, here is a solution if you already used c++ in your game:

First, instead of getting HitResultUnderMouse you could create a virtual plane on every click, so you can project coordinates always at same height as player is. Evengard did Maths for it already, so it should be quite easy to incorporate into your code (https://answers.unrealengine.com/questions/20858/set-collision-settings-for-brushes.html) :slight_smile:

Afterwards we need to do something to PlayerController. First, in .h of your playercontroller, add 2 components:

private:
/** Component used for pathfinding and querying environment's navigation. */
UPROPERTY()
TSubobjectPtr<class UNavigationComponent> NavComponent;

/** Component used for moving along a path. */
UPROPERTY()
TSubobjectPtr<class UPathFollowingComponent> PathFollowingComponent;

Then we need to initialize them on instantiation of controller, so in your constructor-method you need to call them.

	// set up navigation component
	NavComponent = PCIP.CreateDefaultSubobject<UNavigationComponent>(this, TEXT("NavComponent"));

	PathFollowingComponent = PCIP.CreateDefaultSubobject<UPathFollowingComponent>(this, TEXT("PathFollowingComponent"));

Ok, so far so good. Now comes fun part. I’ve bound following method for LeftMouseClick:

void APlayGroundPlayerController::MoveToMouseCursor() {
	FVector dest;
	GetMouseClickCoords(dest);

	UNavigationComponent* PathFindingComp = FindComponentByClass<UNavigationComponent>();
	UPathFollowingComponent* PathFollowComp = FindComponentByClass<UPathFollowingComponent>();
	if (PathFindingComp != NULL && PathFollowComp != NULL) {
		bool bFoundPath = PathFindingComp->FindPathToLocation(dest, NULL);
		if (bFoundPath == false || PathFindingComp->GetPath().IsValid() == false) {
			bFoundPath = PathFindingComp->FindSimplePathToLocation(dest);
		}
		if (bFoundPath == true && PathFindingComp->GetPath().IsValid() == true) {
			PathFollowComp->RequestMove(PathFindingComp->GetPath(), NULL, 100.0f);
		}
	}
}

(GetMouseClickCoords is my method where i pasted code from Evengard) What are we doing in this method now:
First we try to find a path to destination location through navmesh (FindPathToLocation). If we couldnt find a path, or path is not valid for whatever reason, we try to find a “path” without navmesh, which means, player just moves in direction of click. Then we request actual move.

Hope this was understandable and helps any others in future :slight_smile:

Cheers,