Move it smoothly or how to use VInterpTo

Hi!

I’m developing a Tetris clone game with Unreal 5.3.2 and C++.

I use this code to move the block, making it fall:

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

	const FDateTime CurrentDateTime = FDateTime::Now();
	UE_LOG(LogTemp, Log, TEXT("TICK - %s"), *CurrentDateTime.ToString());

	FHitResult OutSweepHitResult;

	SetActorLocation(GetNewLocation(), true, &OutSweepHitResult);

	if (OutSweepHitResult.IsValidBlockingHit())
	{
		UE_LOG(LogTemp, Warning, TEXT("SWEEP Block HIT - %s"), *FDateTime::Now().ToString());
		SetActorTickEnabled(false);

		if (OnStop.ExecuteIfBound())
		{
			UE_LOG(LogTemp, Warning, TEXT("ExecuteIfBound - %s"), *FDateTime::Now().ToString());
		}
		else
		{
			UE_LOG(LogTemp, Warning, TEXT("NO ExecuteIfBound - %s"), *FDateTime::Now().ToString());
		}
	}
}

FVector ATBlock::GetNewLocation() const
{
	const FVector CurrentLocation = GetActorLocation();

	return FVector(CurrentLocation.X, CurrentLocation.Y, CurrentLocation.Z - SpaceToMove);
}

But I want to move the block smoothly.

I think I have to use VInterpTo inside the Tick event, but inside this event it’s where I move the block and I don’t know how to do it.

How can I do it?

Thanks!

You can use VInterpTo or VInterpConstantTo and control the speed with InterpSpeed

const float InterpSpeed = 5.f;
SetActorLocation(FMath::VInterpConstantTo(GetActorLocation(), GetNewLocation(), DeltaTime, InterpSpeed), true, &OutSweepHitResult);
1 Like

While debugging make the interp speed a property that can be edited inside unreal so you dont have to keep flickering back and forth the performance gained from making it a value in the code will be miniscule.

And make it a const in your header then you can perform logic on it down the track when you want to speed it up to make the game harder

Thanks, for your answer. But, if I use it, the block doesn’t move.

I have changed the line:

SetActorLocation(GetNewLocation(), true, &OutSweepHitResult);

With the line:

SetActorLocation(FMath::VInterpConstantTo(GetActorLocation(), GetNewLocation(), DeltaTime, 0.5f), true, &OutSweepHitResult);

And it doesn’t move.

I have also checked FMath::VInterpTo, and it moves, but slower than if I use SetActorLocation(GetNewLocation(), true, &OutSweepHitResult);.

Thanks.

If you use VInterpConstantTo set the interp speed to something higher like 200