Spent way too much time to move a platform, lol

I settled on using a Lerp and no vector math. It moves between two points as expected. I had some issue with it drifting the first time I coded it up because I was modifying the Start and Target Locations in tick. Additionally, I wanted to have a speed easement as the platform approached the end Locations to avoid an abrupt turnaround, but I couldn’t quite get it and I’ve spent too much time on this. I would need to google how to set up a TimeLine, but I tried with Sin(). I’ve had better outcomes with sequencer or the TimeLine BP node. This seems like a common enough procedure that I’m excited to see a best practice for it in c++!

Here’s the code: All the commented out code was things I was trying, like a grave yard of bad ideas, lol.

		if (MovementProgress > 1.f || MovementProgress < 0.f) bIsReturning = !bIsReturning;
		MovementProgress = FMath::Clamp(MovementProgress, 0.f, 1.f);
		MovementProgress += (bIsReturning ? 1 : -1) * (Speed * DeltaTime) / FVector::Dist(StartLocation, TargetLocation);
		SetActorLocation(FMath::Lerp(StartLocation, TargetLocation, MovementProgress));
		// float NormalizedProgress = FMath::Sin((MovementProgress - 0.5f) * 2.0f);
		// NormalizedProgress = (NormalizedProgress + 1.f) * 0.5f;
		// reminder: .9 is slow, and .1 is fast
		// float NormalizedProgress = FMath::Sin(MovementProgress * HALF_PI);
		// MovementProgress += (bIsReturning ? -1.0f : 1.0f) * (DeltaTime / (FVector::Dist(StartLocation, TargetLocation) / Speed));
		// FVector Direction = (bIsReturning ? StartLocation - TargetLocation : TargetLocation - StartLocation).GetSafeNormal();
		// MovementProgress = bIsReturning? MovementProgress + (Speed * DeltaTime) / FVector::Dist(StartLocation, TargetLocation) : MovementProgress - (Speed * DeltaTime) / FVector::Dist(StartLocation, TargetLocation);
		
		// MovementProgress += (Speed * DeltaTime) / FVector::Dist(StartLocation, TargetLocation);
		// FVector Direction = FVector();
		// if (!bIsReturning)
		// {
		// 	Direction = (TargetLocation - StartLocation).GetSafeNormal(); // TargetLocationLocal.GetSafeNormal();
		// 	SetActorLocation(GetActorLocation() + Direction * Speed * DeltaTime);
		// 	FVector::
		// 	if (FVector::Dist(TargetLocation, GetActorLocation()) < 1.0) bIsReturning = true;
		// }

That’s an interesting solution.

The vector maths is kind of important in 3D work. It’s not difficult. You can easily work out a direction vector and then you multiply by speed x delta time. You can check the distance to travel vs the total remaining distance and change the direction when it arrives at the end point.

It is covered in the course.

1 Like

Privacy & Terms