How to set up AController Inputs for Pawn?

Hey I am pretty knew and have tried finding a simple answer to this in the forums but haven’t been able to. If someone can help by looking at my code I would greatly appreciate it.

Scenario:
I want to create a controller actor that handles all of my pawns movements. So all of my Axis mapping and functions are in my controller files. This is what some of my code looks like :

void ABasePlayerController::SetupPlayerInputComponent(UInputComponent* PlayerInputComponent)
{
	//bind inputs here
	PlayerInputComponent->BindAxis("MoveForward", this, &ABasePlayerController::MoveForward);
	PlayerInputComponent->BindAxis("MoveRight", this, &ABasePlayerController::MoveRight);
}

//axis functions
void ABasePlayerController::MoveForward(float AxisValue)
{
	ABasePawn* ControlledPawn = Cast<ABasePawn>(GetPawn());
	ControlledPawn->AddMovementInput(GetActorForwardVector() * AxisValue);

I get an error after trying to compile that says:

'AController::GetActorForwardVector': cannot access private member declared in class 'AController'

Im not really sure what that means and how to fix it. Also, Im not really sure if this is the correct way to bind inputs from a controller to a pawn. Let me know if you can help :slight_smile:

Thank you in advance!

We put all the math for “MoveForward()” or “MoveLeft()” in the pawn / character itself. Encapsulation-wise it makes sense because the PlayerController is just feeding a possessed pawn the player’s commands. The PlayerController should not need to know a Pawn’s forward vector to tell it “Move Forward”.

Example here is a first person character possessed by the player controller:

void AFirstPersonCharacter::MoveForward(float value)
{
	AddMovementInput(GetActorForwardVector(), value * MOVE_FORWARD_RATE * _sprintMultiplier);
}
1 Like

You are getting the controller’s forward vector, not the pawn’s.

However I would recommend you to reconsider. Inputs related to the pawn are intended to be added on the pawn, while inputs related to the player (navigating menus etc.) are added to the controller.

1 Like