math problem?

i have actors on a grid. they move through a component with the following code:

void UGridActorMoveComponent::LerpMove(FTransform source, FTransform destination)
{
	Source = source;
	Destination = destination;

	MoveAlpha = .0f;
	bIsMoving = true;
}

and

void UGridActorMoveComponent::TickComponent(float DeltaTime, ELevelTick TickType, FActorComponentTickFunction* ThisTickFunction)
{
	Super::TickComponent(DeltaTime, TickType, ThisTickFunction);

	AChipsGuy* chipsguy = Cast<AChipsGuy>(GetOwner());

	if (chipsguy && bIsMoving)
	{
		if (MoveAlpha <= 1.f)
		{
			FTransform transform = Source;
			FVector location = FMath::Lerp(Source.GetLocation(), Destination.GetLocation(), MoveAlpha);
			transform.SetLocation(location);
			chipsguy->SetActorTransform(transform);

			MoveAlpha += chipsguy->MoveSpeed * DeltaTime;
		}
		else if (MoveAlpha > 1.0f)
		{
			bIsMoving = false;
			chipsguy->MoveDone();
		}	
	}
}

The actor sets up Source and Destination and Movespeed, the calls LerpMove to initiate the move.
The problem now is, that it is not smooth. it always jumps a bit at the end of the move. This is very noticeable if there are repeated moves in one direction.
The Destination and Source i am sure are correct. i let them be printed on screen and they align perfectly. the end location of one move is the begin location of the next.
Can somebody help how to make the moving smooth?

the MoveAlpha might exceed 1
you should clamp the MoveAlpha from 0 to 1 and always set the location

	AChipsGuy* chipsguy = Cast<AChipsGuy>(GetOwner());

	if (chipsguy && bIsMoving)
	{
		MoveAlpha = FMath::Clamp(MoveAlpha + chipsguy->MoveSpeed * DeltaTime, 0, 1);

		FTransform transform = Source;
		FVector location = FMath::Lerp(Source.GetLocation(), Destination.GetLocation(), MoveAlpha);
		transform.SetLocation(location);
		chipsguy->SetActorTransform(transform);
		
		if (MoveAlpha == 1.0f)
		{
			bIsMoving = false;
			chipsguy->MoveDone();
		}	
	}

Thank you, that is much better.
It still shakes a little bit but i added a spring arm so its barely noticable.

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.