Different Approach for the bonus activity

Instead of doing like was done in the video, I made the following:

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

	float Speed;

	if(bShouldMove)
	{
		FVector TargetLocation = OriginalLocation + MoveOffset;
		Speed = FVector::Distance(OriginalLocation, TargetLocation) / MoveTime;
		MoveToLocation(DeltaTime, TargetLocation, Speed);
	}
	else
	{
		FVector StartLocation = OriginalLocation + MoveOffset;
		Speed = FVector::Distance(StartLocation, OriginalLocation) / MoveTime;
		MoveToLocation(DeltaTime, OriginalLocation, Speed);
	}
}
void UMover::MoveToLocation(float DeltaTime, FVector ToLocation, float Speed)
{
	FVector CurrentLocation = GetOwner()->GetActorLocation();
	FVector NewLocation = FMath::VInterpConstantTo(CurrentLocation, ToLocation, DeltaTime, Speed);

	GetOwner()->SetActorLocation(NewLocation);
}

I did not see any difference between my result and the answer, but I’m curious about what you guys thinl.

It works the same because it is the same code.

Aside from the separate function (which is a good thing), the biggest difference is the seemingly different speed computation. But when you look at that in details, you’ll see that you just wrote the same code twice. You changed the name of the FVector (which has no effect on the functionality) and swapped the order of the parameters for Distance (which has no effect either, since “distance from A to B” == “distance from B to A)”.

I think the issue is that you’re thinking that it matters which end of the mover if the beginning and which one is not. Or that it matters where the mover is coming from. But really, it’s irrelevant. What matters is that the mover has a current location, and has two endpoints, A and B, one of which is where the mover must go to.

So the code ought to limit itself to choosing if A or B is the destination/target, and have everything else as shared code. And that’s what the video does.

Privacy & Terms