How do I get input axis values in my PlayerController? [Tried everything in this thread][1], but nothing worked.
PlayerInput contains a number of structs pertaining to input axes and contains most of what you’re probably looking for. You can additionally find axis mappings in InputSettings.
Actor has a function called GetInputAxisValue, which is probably used by those same blueprint nodes, so you may well be able to use that directly dependent on your needs.
Ok, I figured it out with the help of this:
https://docs.unrealengine.com/latest/FRA/Programming/Gameplay/Framework/Input/index.html
First you need to set up your axis mappings as per the above link. Then in your PlayerController add these functions:
##.h
virtual void BeginPlay() OVERRIDE;
virtual void MoveVertical(float value);
virtual void MoveHorizontal(float value);
##.cpp
void AYourPlayerController::BeginPlay()
{
Super::BeginPlay();
InputComponent->BindAxis("MoveVertical", this, &AYourPlayerController::MoveVertical);
InputComponent->BindAxis("MoveHorizontal", this, &AYourPlayerController::MoveHorizontal);
}
void AYourPlayerController::MoveVertical(float value)
{
if (GEngine)
{
GEngine->AddOnScreenDebugMessage(-1, 5.f, FColor::Yellow, FString("Vertical: ") + FString::SanitizeFloat(value));
}
}
void AYourPlayerController::MoveHorizontal(float value)
{
if (GEngine)
{
GEngine->AddOnScreenDebugMessage(-1, 5.f, FColor::Yellow, FString("Horizontal: ") + FString::SanitizeFloat(value));
}
}
The only caveat I noticed is that if you’re binding your move controls to your MouseX and MouseY like me, if you have bShowMouseCursor = true;
it won’t register any input unless you have a mouse button held down. And obviously make sure your axis mappings match whatever name you’re giving them in code.