Simple mechanics for applying smooth movement to character

I have several completely unrelated mechanics (dodging and flinching being the big two) which require me to move a character smoothly in a desired direction for a desired distance. I originally did this via CharacterMovement->Velocity += desiredChange;, but applying velocity directly to the movement component was jarring and had a very whiplash-y feeling to it. As an alternative I tried calculating a vector n units in front of the player, and lerping towards it in tick:


            float Alpha = 0.5f;             
            destinationLocation = FMath::Lerp(myActor->GetActorLocation(), endLocation, Alpha);
            myCharacter->SetActorLocation(destinationLocation);

This didn’t have the whiplash problem, but due to the use of SetActorLocation movement appeared very jerky, and it became possible to easily teleport through level geometry and other actors. If lerping and directly adding velocity to the controller don’t work, I’m kind of stuck on alternatives, but this must be over-complicating the problem in my head; this is such a fundamental and useful functionality that there must be a standard, or at least reasonably common method of accomplishing it- is there something super-obvious that I’m missing?

I believe there is a blueprint function called Move Component To.

You can also use FMath::VInterp instead of Lerp. Give it the current position, target, deltatime and speed. You should also probably set bSweep to true in the SetActorLocation to stop it going through the environment.

Oh hey, that looks much better, thank you for the idea! I’m always a little leery of using SetActorLocation instead of sending input to the Controller and letting everything be sorted out there, but that looks really great.

This is probably going to betray how bad my vector math is, but am I correct that if I want to give it a destination n units in front of the player’s current location, I would get that by doing this?



float moveDistance = 20; //Distance to traverse in uu
endLocation = myActor->GetActorForwardVector()*moveDistance; //Set the destination to a position float moveDistance unreal units forward of our current facing

I’m getting some weirdness with vinterp, where the actor sometimes shoots off at funny angles or gets sucked backwards, and the only thing I can think of is that the above math doesn’t give me the vector I think it does.

If you want the character to move only forward then you don’t need VInterp.

Try something like this instead:

const float moveSpeed = 2000.f;
myActor->SetActorLocation( myActor->GetActorLocation() + ((myActor->GetActorForwardVector() * moveSpeed) * GetWorld()->GetDeltaTimeSeconds()), true );

That works perfectly; thank you so much! :slight_smile: