Download

Binding multiple axes to a single function for character movement?

Hello, I’m trying to make a character move depending on the “MoveForward” and “MoveRight” Axis Input. The movement works fine when I use UInputComponent::BindAxis() for both axes. In this case I have two functions, MoveForward(float val) and MoveRight(float val).

I’d like to access input from both axes in a single function called MoveXY( float dx, float dy ), so that I can construct a vector indicating the direction of movement input, then I can add a turning effect using that vector. Since I’m working with a static camera, the character orientation will depend on its movement. My thoughts is to have a local FVector, and let the two movement functions affect that FVector instead of character movement, then I can add character movement and the turning effect in the Tick() function. Is it practical?

Hi! This video from Unreal shows how you can do that ( starts at 24m ):

You can’t bind two axes to one function, but the way you get around this is keeping a member variable of your input, accumulating input from each function, then “consuming” it once movement runs. This is exactly how movement input works for the character class (you can see APawn::ConsumeInuputVector for an example).



void MoveForward(float Val)
{
InputVector.X += Val;
}

void MoveRight(float Val)
{
InputVector.Y += Val;
}

void TickMovement()
{
FVector ThisFrameInput = InputVector;
InputVector = FVector();

// run code using ThisFrameInput.
}


Hi @SolidSk, thank you for your recommendation. The video is quite informative.

And thank you @TheJamsh, I was adding that vector using APawn::AddMovementInput() which takes the movement vector as acceleration, but now I found the APawn::ConsumeMovementInputVector() might be a better way to handle it.