if (!RecoilValue.IsZero())
{
RecoilValue = FMath::Vector2DInterpTo(RecoilValue, FVector2D(0.f), DeltaTime, 12.f);
Player->AddControllerYawInput(RecoilValue.X);
Player->AddControllerPitchInput(-RecoilValue.Y);
}
This is my gun recoil code, and it’s in Tick().
It works well as I want, but I have a question while using InterpTo().
If the variable interpolated by InterpTo() is interpolated again, I think it only gets infinitely close to zero(Target) and cannot be zero(Target).
However, when I printed out the log, I could see that it became smaller and then zero.
So I looked inside the interpTo() function, it was as follows.
CORE_API FVector2D FMath::Vector2DInterpTo( const FVector2D& Current, const FVector2D& Target, float DeltaTime, float InterpSpeed )
{
if( InterpSpeed <= 0.f )
{
return Target;
}
const FVector2D Dist = Target - Current;
if( Dist.SizeSquared() < UE_KINDA_SMALL_NUMBER )
{
return Target;
}
const FVector2D DeltaMove = Dist * FMath::Clamp<float>(DeltaTime * InterpSpeed, 0.f, 1.f);
return Current + DeltaMove;
}
I don’t really understand what SizeSquared() is about, but if this starting point and the target point are very close, it seems to just return the target point.
Is my conclusion correct? I am confused because I am not good at math.
It’s awkward to talk because I turned on the translator. Sorry.