I wasn’t happy with the way we are doing the linear interpolation (we are not, in fact, interpolating linearly)
So I search for some way to use curves and discovered this UCurveFloat
Basically you create a Curve in the Editor (on your UE’s Content Browser -> Right-click -> Miscellaneous -> Curve -> CurveFloat), then you open it and create your desired curve going from 0 to 1 in x axis (we can extend this through code)’ and from 0 to 90 in y axis.
And don’t forget to drag and drop the curve in the OpenDoor component inside Unreal Editor. Ideally, we want to check if the pointer to the curve isn’t null (otherwise your editor will crash due to the null pointer ref)
// OpenDoor.h
#include "Curves/CurveFloat.h"
private:
UPROPERTY(EditInstanceOnly)
UCurveFloat* YawCurve;
UPROPERTY(EditAnywhere)
// Time used to extend the opening of the door
float OpeningDuration = 3.5f;
FRotator InitialRotation;
bool bIsOpen;
float ElapsedTime;
// OpenDoor.cpp
void UOpenDoor::BeginPlay()
{
Super::BeginPlay();
InitialRotation = GetOwner()->GetActorRotation();
ElapsedTime = 0;
bIsOpen = false;
}
void UOpenDoor::TickComponent(float DeltaTime, ELevelTick TickType, FActorComponentTickFunction* ThisTickFunction)
{
Super::TickComponent(DeltaTime, TickType, ThisTickFunction);
if (bIsOpen)
{
return;
}
if (ElapsedTime > OpeningDuration)
{
bIsOpen = true;
ElapsedTime = 0.f;
return;
}
ElapsedTime += DeltaTime;
FRotator CurrentRotation = GetOwner()->GetActorRotation();
auto CurrentYaw = YawCurve->GetFloatValue(ElapsedTime / OpeningDuration);
CurrentRotation.Yaw = InitialRotation.Yaw + CurrentYaw;
GetOwner()->SetActorRelativeRotation(CurrentRotation);
}