Move The Platform Without Vector Math

I took the easy way out and used a boolean and VInterpConstantTo() function.

AMovingPlatform::AMovingPlatform()
{
// Set this actor to call Tick() every frame. You can turn this off to improve performance if you don’t need it.
PrimaryActorTick.bCanEverTick = true;
Speed = 5.0;
//replicate the moving platform on all player machines
bReplicates = true;
SetReplicateMovement(true);

SetMobility(EComponentMobility::Movable);			//make platform movable
bMoveToEndPt = true;							//begin by moving towards the end pt.

}

void AMovingPlatform::BeginPlay()
{
Super::BeginPlay();
StartPt = GetActorLocation();
EndPt += StartPt; //EndPt is a relative location, we need the world location
}

void AMovingPlatform::Tick(float DeltaTime)
{
Super::Tick(DeltaTime);

if (HasAuthority())
{
	FVector currentLoc = GetActorLocation();
	FVector targetLoc = bMoveToEndPt ? EndPt : StartPt;

	if (!currentLoc.Equals(targetLoc))
	{
		FVector newLoc = FMath::VInterpConstantTo(currentLoc, targetLoc, DeltaTime, Speed);
		SetActorLocation(newLoc);
	}
	else
	{
		bMoveToEndPt = !bMoveToEndPt;             //reverse the movement
	}
}

}

1 Like

Privacy & Terms