Looking at the API, this is my approach:
if ((GlobalTargetLocation - Location).IsUnit(Speed*DeltaTime)) {
Swap(GlobalTargetLocation, GlobalStartLocation);
}
The idea is to check if the distance between the target and the current location is smaller than the “Speed*DeltaTime” step-width that we would go in the next step in order to prevent overshooting the target. If it is, I reverse the direction using “Swap”.
Performance-wise this is probably not the best solution, but I like the idea that its a pretty “clean” two-liner (not counting the brackets)
The full tick() method looks like this:
void AMovingPlatform::Tick(float DeltaTime)
{
Super::Tick(DeltaTime);
if (HasAuthority())
{
FVector Location = GetActorLocation();
if ((GlobalTargetLocation - Location).IsUnit(Speed*DeltaTime)) {
Swap(GlobalTargetLocation, GlobalStartLocation);
}
FVector Direction = (GlobalTargetLocation - GlobalStartLocation).GetSafeNormal();
Location += Speed * DeltaTime * Direction;
SetActorLocation(Location);
}
}