Here’s how I did it. I think it’s a little more simple. But I did like how the solution after the challenge break uses more linear algebra.
First I added an FVector originalLocation;
to the class.
Then in BeginPlay
the original location is saved when the actor begins.
void AMovingPlatform::BeginPlay()
{
Super::BeginPlay();
originalLocation = GetActorLocation();
if (HasAuthority()) {
SetReplicates(true);
SetReplicateMovement(true);
}
}
Finally Tick uses the original location, so it doesn’t need to translate the vector from World space to Local space.
Also I used GetWorld()->RealTimeSeconds
because it counts from when the game starts up, so then when I sine it, it will oscillate between the two points smoothly.
void AMovingPlatform::Tick(float deltaSeconds)
{
Super::Tick(deltaSeconds);
if (!HasAuthority())
return;
auto world = GetWorld();
auto gameTime = world->RealTimeSeconds;
auto location = originalLocation + (TargetLocation * sin(gameTime));
SetActorLocation(location, false);
}