Lerp not working the same in C++ as it does in Blueprints

I am working on the SimpleShooter course and wanted to try implementing a sprint function. When the left thumbstick or left shift is pressed, the MaxWalkSpeed is set to 600 and when the action is released, the MaxWalkSpeed is set back to 300. I noticed when releasing the Sprint button, the animation is instant from jog to walk. After doing some research, I learned how to use Lerp in blueprints and it looks perfect, however I can’t seem to get it to work in C++.

Here is how I implemented it in Blueprints

and here’s the Sprint, StopSprinting, and AdjustMaxMoveSpeed functions in my c++

void AShooterCharacter::Sprint()
{
#if 0
TargetMovementSpeed = 600.f;
//GetCharacterMovement()->MaxWalkSpeed = TargetMovementSpeed;
#endif
}

void AShooterCharacter::StopSprinting()
{
#if 0
TargetMovementSpeed = 300.f;
//GetCharacterMovement()->MaxWalkSpeed = TargetMovementSpeed;
#endif
}

void AShooterCharacter::AdjustMaxMoveSpeed()
{
#if 0
float CurrentWalkSpeed = GetCharacterMovement()->MaxWalkSpeed;
float Difference = abs(CurrentWalkSpeed - TargetMovementSpeed);
if (Difference > 1)
{
CurrentWalkSpeed = FMath::Lerp(CurrentWalkSpeed, TargetMovementSpeed, 0.1f);
}
#endif
}

the if/endif is just there for me to turn on/off while working on the next parts of the course. in the header, TargetMovementSpeed is a float with default value 300.f. My includes are all there and it compiles. The character just never sprints when using AdjustMaxMoveSpeed. When I use

GetCharacterMovement()->MaxWalkSpeed = TargetMovementSpeed;
it works with the bad transition.

Any advice would be great and if you need any more info, I would be happy to give it.

So, What I’m assuming you want to do is ramp up the speed between the current speed and max speed?

Anyway, I don’t see a Set to the MaxWalkSpeed in the C++ code. Could that be it?

Might be worth printing the values out to see how they compare but I guess they are the same. The Blueprint Lerp node is actually calling that exact C++ Lerp so there’s no reason why the lerp itself is the issue.

1 Like

I was originally using

void AShooterCharacter::AdjustMaxMoveSpeed()
{
float Difference = abs(GetCharacterMovement()->MaxWalkSpeed - TargetMovementSpeed);
if (Difference > 1)
{
GetCharacterMovement()->MaxWalkSpeed = FMath::Lerp(GetCharacterMovement()->MaxWalkSpeed, TargetMovementSpeed, 0.1f);
}
}

I switched the MaxWalkSpeed to a float CurrentWalkSpeed. I realized this morning that I was no longer affecting the MaxWalkSpeed in the Movement Component so I switched it back and magically it works now…

I deleted my binaries, saved and intermediate files, regenerated vs project files and tried again. Maybe that was it, maybe something else in my code was wrong. I’m not entirely sure but I’m glad it works.

Thank you @beegeedee

that was exactly what I needed to do

1 Like

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

Privacy & Terms