# Smooth MaxWalkSpeed change

Hello, I have pretty simple sprint functionality, which sets MaxWalkSpeed to higher value while key is pressed and resets to original when released. How can I ease out this value, so that my animation wont look clumsy? What is correct approach to this?

Set timer on a function which loops and increases/decreases movement speed up to needed point by, for example, 1 point.

Not sure its good idea. Very easy to mess with timers when you want to use spring few times in a row, while timer still works.

I would use the Tick function, since you want it to run at every frame assuming that this is for your main character (the avatar of the player), so your animation needs to be as smooth as possible.

Here just a sketch/idea of what the code could look like.

You can have the following variables:

``````bool bIsKeyPressed; //when your key is pressed, this bool will be true.
float OriginalMaxWalkSpeed; //The Max Walk Speed when the key is not pressed
float CurrentMaxWalkSpeed; //The Max Walk Speed that the player is currently having
float LimitMaxWalkSpeed; //I called it Limit, since Max is already in the name, but it is the maximum MaxWalkSpeed you can reach
float TransitionSpeed; //How fast the transition is
``````

Then, in the Tick function have something like this:

``````if(!bIsKeyPressed)
{
CurrentMaxWalkSpeed = OriginalMaxWalkSpeed;
return;
}

//Check if close
if(LimitMaxWalkSpeed - CurrentMaxWalkSpeed  <= 0.001)
{
CurrentMaxWalkSpeed  = LimitMaxWalkSpeed;
return;
}

//Any smoothing function you want. Here just a simple linear
CurrentMaxWalkSpeed  = FMath::Lerp(CurrentMaxWalkSpeed, LimitMaxWalkSpeed, FMath::Clamp(DeltaTime * TransitionSpeed, 0.f, 1.f))

//Some code to set the MaxWalkSpeed to CurrentMaxWalkSpeed
// [...]
``````

This code isn’t tested, and I wrote it without the editor in front of me, but it should contain the main concept. If you need a hand to write the actual code that compiles and include also the input detection, let me know and I will try to help you.

Also, why do you want to change the MaxWalkSpeed?

Yeah, I like this idea. How would it affect performance?
Also is it possible to use timeline and set speed based on its movement, so that it would be done inside movement method rather than in tick?

Since it’s C++ performance wise, the method is fast. Moreover there is a check at the beginning to shave extra bit of performance. As I said, since it is an animation related (especially if it is your player character), you want to run it every frame.

If you want, you can also use a timeline, and evaluate values from there. Performance wise, the more operations you do, the less performance you will have. However, you should not worry too much about performance if the operation is so simple, and done in C++. Evaluating a Timeline for the player character is more than reasonable performance price.

I am not sure what do you mean with “moment” method.

Ok, that clears things out. I will try this approach, thanks!

SO I’ve implemented this and it worked great. I had to readjust a little bit, but overall idea was perfect, thanks!