Lerp Not Working Properly

Hi! For some reason, my lerp isn’t working properly although I am sure I have not made any mistakes. It was always stuck around 45 and my target is 90.

Here is a function I wrote which combines both Open and Close. My Tick and BeginPlay. My H File. And lastly my Log .

What is the initial yaw?


As an aside, “OpenAndClose” implies it’s going to do both at the same time. I think a better name would be OpenOrClose, or better yet IMO “RotateDoor”.

That function also 90% the same for both cases the one difference is the second argument. You could instead create a variable for that (also you’re missing the b prefix).

float ToYaw;
if (bDoorCose)
{
    ToYaw = InitialYaw;
}
else
{
    ToYaw = TargetYaw;
}
CurrentYaw = FMath::Lerp(CurrentYaw, ToYaw, ...);
// etc...

Or more succinctly use the ternary operator (aka conditional operator).

const float ToYaw = bCloseDoor ? InitialYaw : TargetYaw;
//                 ^-condition-^  ^if true   ^if false

My InitialYaw is 0. And wow, your code is really clean! I will follow your advice, really makes my code easier to read, thanks! And FYI, the door isn’t always stuck at 45, it changes depending on what value I put in the Alpha parameter for the lerp function.

Are you sure, did you print out its value in BeginPlay?

Yup, very sure. Just did so

And just to be sure, the TargetYaw? If that’s the expected value would you mind sending me your project using the following link?

https://gdev.tv/projectupload

Please use File > Package Project > Zip Up Project within Unreal as this will ensure only required files are zipped up and things like the Binaries are excluded.

Alright, no problem, thanks. I submitted

image

So what you sent works as expected. Only issue was that you didn’t call SetActorRotation.

float ToYaw;
if (bDoorClose)
{
	ToYaw = InitialYaw;
}
else
{
	ToYaw = TargetYaw;
}
CurrentYaw = FMath::Lerp(CurrentYaw, ToYaw, 0.2f * DeltaTime);
GetOwner()->SetActorRotation(FRotator(0.f, CurrentYaw, 0.f)); // This was missing

Oops, I accidentally commented SetActorRotation out. I changed my code just like how you recommended, but I haven’t tried it out yet, as I have been busy, but now I did it works as expected. So the problem must lie within my old code. I went to have a look at my old code, the one I posted at the beginning, and recreated the code and found the mistake. I forgot to put else, so that causes my CurrentYaw to increase, and then to decrease again, my bad. Thanks and sorry for taking your time.

This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.

Privacy & Terms