Skip to content

Instantly share code, notes, and snippets.

@michaeltchapman
Created March 26, 2020 02:56
Show Gist options
  • Save michaeltchapman/c7caa2394ee059b3c6c0de31d6fd1586 to your computer and use it in GitHub Desktop.
Save michaeltchapman/c7caa2394ee059b3c6c0de31d6fd1586 to your computer and use it in GitHub Desktop.
void AWorldMapCharacter::MoveToDestination()
{
if (bUpdateDestination || !NavPath)
{
NavPath = UNavigationSystemV1::FindPathToLocationSynchronously(this, GetActorLocation(), Destination, this);
if (NavPath != nullptr)
{
CurrentPathIndex = 1;
if (Debug)
{
for (auto &PathPoint : NavPath->PathPoints)
{
DrawDebugSphere(GetWorld(), PathPoint, 50.0f, 8, FColor::Cyan, false, 20.0f);
}
DrawDebugSphere(GetWorld(), Destination, 50.0f, 8, FColor::Orange, false, 5.0f);
}
}
}
if (NavPath)
{
if (NavPath->PathPoints.Num() > CurrentPathIndex)
{
if (FVector::Dist(GetActorLocation(), NavPath->PathPoints[CurrentPathIndex]) < PathPointAcceptanceDistance)
{
CurrentPathIndex++;
}
}
if (NavPath->PathPoints.Num() > CurrentPathIndex)
{
FVector InputVector = NavPath->PathPoints[CurrentPathIndex] - GetActorLocation();
InputVector.Z = 0.0f;
InputVector.Normalize();
AddMovementInput(InputVector);
}
}
if (FVector::Dist(GetActorLocation(), Destination) > MinimumMoveDistance)
{
bMoveToMouseCursor = true;
}
else {
bMoveToMouseCursor = false;
}
}
void AWorldMapCharacter::UpdateDestination()
{
float MouseX;
float MouseY;
APlayerController *MyPlayerController = Cast<APlayerController>(GetController());
// Get screen space location
if (MyPlayerController)
{
MyPlayerController->GetMousePosition(MouseX, MouseY);
FVector2D MousePosition = FVector2D(MouseX, MouseY);
FHitResult HitResult;
MyPlayerController->GetHitResultAtScreenPosition(MousePosition, COLLISION_OVERRIDEAIMING, true, HitResult);
if (!HitResult.bBlockingHit)
{
MyPlayerController->GetHitResultAtScreenPosition(MousePosition, COLLISION_AIMING, true, HitResult);
}
if (HitResult.bBlockingHit)
{
Destination = HitResult.ImpactPoint;
}
//FNavLocation NavDest;
//GetWorld()->GetNavigationSystem()->ProjectPointToNavigation(Destination, NavDest, FVector(100000.0f));
//Destination = NavDest.Location;
if (FVector::Dist(GetActorLocation(), Destination) > MinimumMoveDistance)
{
bMoveToMouseCursor = true;
}
else {
bMoveToMouseCursor = false;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment