Hello,
I want to bind a custom Player Controller class with a Pawn that has a Camera Component in order to make a rts type camera that can be moved with ‘wasd’. I have already made the GodPawn logic, however I can’t find a way to make the MainPlayerController handle the inputs for that camera. Do I need to instanciate the GodPawn in the MainPlayerController class? And if so how it is done?
This is the code I have right now, I’ve also configured the inputs in the project settings for ‘wasd’ as “MoveForward” and “MoveRight”.
GodPawn.h:
#pragma once
#include "CoreMinimal.h"
#include "GameFramework/Pawn.h"
#include "Camera/CameraComponent.h"
#include "GameFramework/SpringArmComponent.h"
#include "GodPawn.generated.h"
UCLASS()
class MYPROJECT2_API AGodPawn : public APawn
{
GENERATED_BODY()
public:
// Sets default values for this pawn's properties
AGodPawn();
protected:
// Called when the game starts or when spawned
virtual void BeginPlay() override;
public:
// Called every frame
virtual void Tick(float DeltaTime) override;
// Called to bind functionality to input
virtual void SetupPlayerInputComponent(class UInputComponent* PlayerInputComponent) override;
protected:
UPROPERTY()
USpringArmComponent* SpringArmComponent;
UPROPERTY()
UCameraComponent* CameraComponent;
};
GodPawn.cpp
#include "GodPawn.h"
// Sets default values
AGodPawn::AGodPawn()
{
// Set this pawn to call Tick() every frame. You can turn this off to improve performance if you don't need it.
PrimaryActorTick.bCanEverTick = true;
RootComponent = CreateDefaultSubobject<USceneComponent>(TEXT("RootComponent"));
RootComponent->SetWorldLocation(FVector(0.0f, 0.0f, 20.0f));
SpringArmComponent = CreateDefaultSubobject<USpringArmComponent>(TEXT("SpringArmComponent"));
SpringArmComponent->SetupAttachment(RootComponent);
SpringArmComponent->SetRelativeLocationAndRotation(FVector(0.0f, 0.0f, 50.0f), FRotator(-60.0f, 0.0f, 0.0f));
SpringArmComponent->TargetArmLength = 400.0f;
CameraComponent = CreateDefaultSubobject<UCameraComponent>(TEXT("CameraComponent"));
CameraComponent->SetupAttachment(SpringArmComponent, USpringArmComponent::SocketName);
//AutoPossessPlayer = EAutoReceiveInput::Player0;
}
// Called when the game starts or when spawned
void AGodPawn::BeginPlay()
{
Super::BeginPlay();
}
// Called every frame
void AGodPawn::Tick(float DeltaTime)
{
Super::Tick(DeltaTime);
}
// Called to bind functionality to input
void AGodPawn::SetupPlayerInputComponent(UInputComponent* PlayerInputComponent)
{
Super::SetupPlayerInputComponent(PlayerInputComponent);
}
MainPlayerController.h:
#pragma once
#include "CoreMinimal.h"
#include "GameFramework/PlayerController.h"
#include "MainPlayerController.generated.h"
/**
*
*/
UCLASS()
class MYPROJECT2_API AMainPlayerController : public APlayerController
{
GENERATED_BODY()
public:
AMainPlayerController();
virtual void Tick(float DeltaTime) override;
protected:
virtual void BeginPlay() override;
public:
virtual void SetupInputComponent() override;
protected:
FVector2D MovementInput;
class AGodPawn* GodPawn;
void MoveForward(float AxisValue);
void MoveRight(float AxisValue);
};
MainPlayerController.cpp:
#include "MainPlayerController.h"
#include "GodPawn.h"
AMainPlayerController::AMainPlayerController()
{
bShowMouseCursor = true;
}
void AMainPlayerController::BeginPlay()
{
GodPawn = Cast<AGodPawn>(GetPawn());
}
void AMainPlayerController::Tick(float DeltaTime)
{
Super::Tick(DeltaTime);
if (GodPawn)
{
if (!MovementInput.IsZero())
{
MovementInput = MovementInput.GetSafeNormal() * 1000.0f;
FVector NewLocation = GodPawn->GetActorLocation();
NewLocation += GetActorForwardVector() * MovementInput.X * DeltaTime;
NewLocation += GetActorRightVector() * MovementInput.Y * DeltaTime;
GodPawn->SetActorLocation(NewLocation);
}
}
}
void AMainPlayerController::SetupInputComponent()
{
Super::SetupInputComponent();
InputComponent->BindAxis("MoveForward", this, &AMainPlayerController::MoveForward);
InputComponent->BindAxis("MoveRight", this, &AMainPlayerController::MoveRight);
}
void AMainPlayerController::MoveForward(float AxisValue)
{
MovementInput.X = FMath::Clamp(AxisValue, -1.0f, 1.0f);
}
void AMainPlayerController::MoveRight(float AxisValue)
{
MovementInput.Y = FMath::Clamp(AxisValue, -1.0f, 1.0f);
}