Hello,
When I spawn a spline my character follows it correctly, but it’s giving a reversed rotation and it’s facing backwards.
Does anyone know why?
I’m using a timeline to update the position on update:
void UComponentFollowSpline::StartSpline()
{
if (!spline || !accelerationCurve) {
return;
}
timeline = NewObject<UTimelineComponent>(this, UTimelineComponent::StaticClass());
FOnTimelineFloat onProgressUpdate;
onProgressUpdate.BindUFunction(this, FName("OnAccelerationTimelineUpdate"));
timeline->AddInterpFloat(accelerationCurve, onProgressUpdate);
FOnTimelineEvent onFinished;
onFinished.BindUFunction(this, FName("OnAccelerationTimelineFinished"));
timeline->SetTimelineFinishedFunc(onFinished);
float newLength = duration;
float curveLength = accelerationCurve->FloatCurve.GetLastKey().Time; // Get the length of the curve
float playRate = curveLength / newLength; // Calculate the playback rate to scale the curve
timeline->SetTimelineLength(newLength);
timeline->SetPlayRate(playRate);
timeline->RegisterComponent();
timeline->Play();
}
This is the code that it’s called when the timeline is ticked:
void UComponentFollowSpline::OnAccelerationTimelineUpdate(float Alpha)
{
if (!spline) {
return;
}
float progress = FMath::Lerp(0, spline->GetSplineLength(), Alpha);
AActor* owner = GetOwner();
FVector position = spline->GetLocationAtDistanceAlongSpline(progress, ESplineCoordinateSpace::World);
FRotator rotation = spline->GetRotationAtDistanceAlongSpline(progress, ESplineCoordinateSpace::World);
owner->SetActorLocation(position);
owner->SetActorRotation(rotation);
}
Thank you!