Download

How to get Mouse.x in Character Controller

When creating a project based on a Third Person Controller, we can see the Mouse Input blueprint that provides the ImputAxis Turn value. This appears to be the raw Mouse.X value that is then used in another function. I’d like to use this raw value in C++ but can’t find how to access it. The Character class has the TurnAtRate(float Rate) function, but this seems to only be populated when a joystick is being used. I’m using mouse only.

How do I capture the raw X value from the mouse in the Character class so I can rotate my character and apply some custom rotation?

I just realized my Player Character was binding Turn to Pawn::AddControllerYawInput(). I added a deligate to capture this value in my derived class, set my local variable, then call the Pawn’s AddControllerYawInput() and that seems to work.

Changed SetupPlayerInputComponent()


//PlayerInputComponent->BindAxis("Turn", this, &APawn::AddControllerYawInput); <-- original from template
    PlayerInputComponent->BindAxis("Turn", this, &AShooterCPPCharacter::TurnValue);

Call the base and set my variable for later use:


void AShooterCPPCharacter::TurnValue(float f)
{
    Super::AddControllerYawInput(f);

    SetTurnAtRateInput(f);
}

Assuming this is the right way to get this value?

1 Like

Looks correct. You “BindAxis” to any function that takes a single float parameter. Then do with that float what you wish. It should range from -1 to 1.

If you want explicit Mouse XY from anywhere its like this (there are multiple ways but this is one):



FVector2D mousePosition;
GetWorld()->GetGameViewport()->GetMousePosition(mousePosition);