I’m making a kind of multiplayer game that my character moves to a point where I clicked. And it would be run on dedicated server.
So I set the NavigationMesh on the level, and checked “Project Settings->Navigation Systems->Allow Client Side Navigation”. And wrote movement code like this:
-MyPlayerController.cpp
void AMyPlayerController::MoveToClickedPoint()
{
FHitResult OutHit;
GetHitResultUnderCursorByChannel(ETraceTypeQuery::TraceTypeQuery1, false, OutHit);
UNavigationSystem::SimpleMoveToLocation(this, OutHit.Location);
}
It works on a single-player mode but doesn’t on a dedicated server. So I tried an alternative way; build a path and make its pawn trace the path points.
-MyCharacter.cpp
void AMyCharacter::SetPath(UNavigationPath* NewNavigationPath)
{
NavigationPath = NewNavigationPath;
TargetPointIndex = 1;
}
void AMyCharacter::MoveToNextPoint()
{
TArray<FVector> PathPoints = NavigationPath->PathPoints;
// End of path
if(PathPoints.Num() <= TargetPointIndex)
{
NavigationPath = nullptr;
return;
}
FVector Dist = PathPoints[TargetPointIndex] - GetActorLocation();
Dist.Z = 0.f;
FVector TargetDirection = Dist.GetSafeNormal(MoveTolerance);
if(TargetDirection.IsNearlyZero())
{
++TargetPointIndex;
// Recall itself to next step
MoveToNextPathPoint();
}
else
{
AddMovementInput(TargetDirection);
}
}
void AMyCharacter::Tick(float DeltaSeconds)
{
Super::Tick(DeltaSeconds);
if(NavigationPath)
{
MoveToNextPathPoint();
}
}
-MyPlayerController.cpp
void AMyPlayerController::MoveToClickedPoint()
{
FHitResult OutHit;
GetHitResultUnderCursorByChannel(ETraceTypeQuery::TraceTypeQuery1, false, OutHit);
UNavigationPath* NavigationPath = UNavigationSystem::FindPathToLocationSynchronously(GetWorld(), GetPawn()->GetActorLocation(), OutHit.Location);
Cast<AMyCharacter>(GetPawn())->SetPath(NavigationPath);
}
It works fine on network but I want to use the “SimpleMoveToLocation”. Is there any way to use it? Should I use RPC? Or any other ways?