c++unreal-engine4unreal-blueprint

UE4-Changing movement speed depending on the direction of movement


I have a third person character. I use controller desired rotation for rotating character with the mouse as in PUBG etc. How can I change max walk speed when I go forward? For example, when I walk forward my speed must be 500 but when I walk right or backward my speed must be 250. How to detect this? I certainly can add if-s on input axis events but my game is multiplayer and this will reduce performance if I check and change speed every tick. Нow to solve it correctly?


Solution

  • I can provide a solution for Blueprints, although I can't be certain it's appropriate in your case.

    The key is to add movement input to the forward vector of your player character. See the screenshot below.

    Blueprint for direction-dependent movement speed

    The AxisValue output from the MoveForward input event is +1 when you press forward and -1 when you press back.

    If your walk speed is 500 then the upper AddMovementInput adds 500 movement, multiplied by 1, in the direction the player is facing.

    The lower one adds 500 movement, multiplied by -1 multipled by 0.5, which will make you go backward (technically, forward with a negative value) at half speed.

    You can apply the same logic to the MoveRight input.

    PS If you're using an analogue controller, I assume AxisValue will be in the range -1 to 1, but that doesn't change the logic above.