x

Search in
Sort by:

Question Status:

Search help

  • Simple searches use one or more words. Separate the words with spaces (cat dog) to search cat,dog or both. Separate the words with plus signs (cat +dog) to search for items that may contain cat but must contain dog.
  • You can further refine your search on the search results page, where you can search by keywords, author, topic. These can be combined with each other. Examples
    • cat dog --matches anything with cat,dog or both
    • cat +dog --searches for cat +dog where dog is a mandatory term
    • cat -dog -- searches for cat excluding any result containing dog
    • [cats] —will restrict your search to results with topic named "cats"
    • [cats] [dogs] —will restrict your search to results with both topics, "cats", and "dogs"

Bind Pawn with PlayerController for handling input

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);
 }







Product Version: UE 4.18
Tags:
more ▼

asked Feb 10 '18 at 05:08 PM in C++ Programming

avatar image

esirumeran
3 2 3

(comments are locked)
10|2000 characters needed characters left

1 answer: sort voted first

Have you changed your default pawn class or gamemode in the editor to use your C++ pawn?

more ▼

answered Feb 11 '18 at 12:16 AM

avatar image

rainb00ts
31 3 2

avatar image esirumeran Feb 11 '18 at 09:25 AM

Yes, that was the problem. Thank you!

(comments are locked)
10|2000 characters needed characters left
Your answer
toggle preview:

Up to 5 attachments (including images) can be used with a maximum of 5.2 MB each and 5.2 MB total.

Follow this question

Once you sign in you will be able to subscribe for any updates here

Answers to this question