So , i’m a beginner with unreal engine 4 and i’m trying to make a player controller what select a pawn with the left mouse click, but when i click on the pawn it doesn’t get possessed.
Also , i would like to don’t make a method in the pawn itself , since i’m trying to make a rts controller, where the player can control multiple pawns , so write the method in every pawn would be annoying , also i prefer to having everything in the player controller. (sorry for my potato english)
This is my code:
MyPlayerController.cpp
#include "RTSTry.h"
#include "MyPlayerController.h"
AMyPlayerController::AMyPlayerController()
{
//Enable Mouse Cursor and Events
bShowMouseCursor = true;
bEnableClickEvents = true;
bEnableMouseOverEvents = true;
bEnableTouchEvents = true;
bEnableTouchOverEvents = true;
if (GEngine) {
GEngine->AddOnScreenDebugMessage(-1, 5.f, FColor::Blue, FString::Printf(TEXT("Controller Initialized")));
}
}
void AMyPlayerController::SetupInputComponent()
{
Super::SetupInputComponent();
InputComponent->BindAction("LeftClick", IE_Pressed, this, &AMyPlayerController::OnLeftClick);
}
void AMyPlayerController::OnLeftClick()
{
FHitResult hit;
GetHitResultUnderCursor(ECC_Pawn, false, hit);
if (hit.bBlockingHit) {
if (hit.Actor != NULL) {
APawn* selectPawn = Cast<APawn>(hit.GetActor());
Possess(selectPawn);
}
}
}
MyPlayerController.h
#pragma once
#include "GameFramework/PlayerController.h"
#include "MyPlayerController.generated.h"
/**
*
*/
UCLASS()
class RTSTRY_API AMyPlayerController : public APlayerController
{
GENERATED_BODY()
AMyPlayerController();
void OnLeftClick();
virtual void SetupInputComponent() override;
};