I am trying to control my character through my player controller.
In my controller’s .h:
public:
// Sets default values
AD17PlayerController();
// Controlled Character
ACharacter* ControlledCharacter;
protected:
// Binds functionality to inputs
virtual void SetupInputComponent() override;
// Input functions
void MoveForward(float AxisValue);
void MoveRight(float AxisValue);
In my controller’s .cpp:
void AD17PlayerController::SetupInputComponent()
{
Super::SetupInputComponent();
InputComponent -> BindAxis("MoveY", this, &AD17PlayerController::MoveForward);
InputComponent -> BindAxis("MoveX", this, &AD17PlayerController::MoveRight);
}
void AD17PlayerController::MoveForward(float AxisValue)
{
ControlledCharacter = Cast<ACharacter>(this -> GetPawn());
if (ControlledCharacter)
{
ControlledCharacter -> MoveForward(AxisValue); // <----- Here is where the error occurs
}
}
In my character’s .h:
UFUNCTION()
void MoveForward(float Val);
In my character’s .cpp:
// Manage input for vertical axis
void AD17Character::MoveForward(float Val)
{
if (GEngine)
{
GEngine->AddOnScreenDebugMessage(-1, 5.f, FColor::Blue, TEXT("Forward"));
}
Val = FMath::Clamp<float>(Val, -1.0f, 1.0f);
if ((Controller != NULL) && (Val != 0.0f))
{
FRotator Rotation = Controller -> GetControlRotation();
if (GetCharacterMovement() -> IsMovingOnGround() || GetCharacterMovement() -> IsFalling())
{
Rotation.Pitch = 0.0f;
}
const FVector Direction = FRotationMatrix(Rotation).GetScaledAxis(EAxis::Y);
AddMovementInput(Direction, Val);
}
}
My idea is to bind the input to functions in the controller and then delegate the axis values to the character who will then do something with the values, in this case move forward.
I believe I am mostly there but when I go to compile I get an error in my controllers .cpp saying that there is “no member named MoveForward in ACharacter” it also doesn’t work when I cast to AD17Character (my character class) I wouldn’t want to do that anyway I don’t think because I want to be able to control different types of characters from that function, so specifying the D17Character would defeat the purpose.
What am I doing wrong here and how can I make it so that the controller is able to manage input for multiple types of character?