
[Unreal Engine] FMath::Lerp
FMath::Lerp esegue un’interpolazione lineare tra due valori, l’Alpha varia da 0 a 1.
In Unreal Engine, FMath::Lerp è una funzione che consente di eseguire una “linear interpolation” (interpolazione lineare) tra due valori. L’interpolazione lineare è un metodo che calcola un valore intermedio tra due estremi basandosi su un fattore di interpolazione compreso tra 0 e 1.
La firma della funzione FMath::Lerp è la seguente:
template <typename T>
static FORCEINLINE T Lerp(const T& A, const T& B, float Alpha);
Dove:
Aè il valore di partenzaBè il valore di destinazioneAlphaè il fattore di interpolazione che determina la quantità di “mescolamento” tra i due valori. Un valore di 0 restituisce il valore di partenzaA, un valore di 1 restituisce il valore di destinazioneB, mentre un valore compreso tra 0 e 1 restituisce un valore intermedio traAeBin base al fattore di interpolazione.
Ad esempio, se si desidera eseguire l’interpolazione lineare tra due valori float StartValue e TargetValue con un fattore di interpolazione di 0.5, si può utilizzare la funzione FMath::Lerp nel seguente modo:
float Result = FMath::Lerp(StartValue, TargetValue, 0.5f);
Il risultato sarà un valore che rappresenta l’interpolazione lineare al 50% tra StartValue e TargetValue.
| Module | Core |
| Header | /Engine/Source/Runtime/Core/Public/Math/UnrealMathUtility.h |
| Include | #include “Math/UnrealMathUtility.h” |
Esempio
Script di esempio per la rotazione.
MioScript.h:
private: float TargetYaw = 10.f; // 360.f float InitialYaw; // 270.f float CurrentYaw;
MioScript.cpp:
// Called when the game starts
void UMioScript::BeginPlay()
{
Super::BeginPlay();
// HERE BEGIN CODE
InitialYaw = GetOwner()->GetActorRotation().Yaw;
CurrentYaw = InitialYaw;
TargetYaw = InitialYaw + 90.f;
}
// Called every frame
void UMioScript::TickComponent(float DeltaTime, ELevelTick TickType, FActorComponentTickFunction* ThisTickFunction)
{
Super::TickComponent(DeltaTime, TickType, ThisTickFunction);
// HERE BEGIN CODE
CurrentYaw = FMath::Lerp(CurrentYaw, TargetYaw, 0.02f);
FRotator DoorRotation = GetOwner()->GetActorRotation();
DoorRotation.Yaw = CurrentYaw;
GetOwner()->SetActorRotation(DoorRotation);
}
I nostri corsi : https://www.develop4fun.it/premium-corsi-online-in-abbonamento
▼ SEGUICI SU ▼
» Facebook: https://www.facebook.com/developforfun
» Instagram: https://www.instagram.com/develop4fun
» Twitter: https://twitter.com/Develop4fun
VISITA IL NOSTRO SITO WEB: https://www.develop4fun.it
ISCRIVITI SUBITO AL NOSTRO CANALE: https://www.youtube.com/channel/UCZ4dhshzpVbbRPVuL9TNH4Q



