ue4 玩家控制器APlayerController
生活随笔
收集整理的這篇文章主要介紹了
ue4 玩家控制器APlayerController
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
通過 APlayerController 可以讓輸入設備控制游戲,可以直接在藍圖中設置,不過我比較喜歡折騰c++(邏輯代碼都是用c++寫)
1、創(chuàng)建一個 AMyPlayerCtrler 繼承自 APlayerController,重寫一些方法
AMyPlayerCtrler.h
#include "MyPlayerCtrler.generated.h"class UMyInput; class UMyCameraComp; class AMySpectator; class AMyChar;UCLASS() class AMyPlayerCtrler : public APlayerController {GENERATED_BODY()public:AMyPlayerCtrler();virtual ~AMyPlayerCtrler();protected:/** True if the controlled character should navigate to the mouse cursor. */uint32 bMoveToMouseCursor : 1;// Begin PlayerController interfacevirtual void PlayerTick(float DeltaTime) override;virtual void SetupInputComponent() override;virtual void ProcessPlayerInput(const float DeltaTime, const bool bGamePaused) override;// End PlayerController interfacevoid OnLeftMousePressed();void OnRightMousePressed();void OnReadAtk();private:TArray<AMyChar*> mSelectedVec;bool mIsReadyAtk;AMyPlayerCtrler.cpp
void AMyPlayerCtrler::SetupInputComponent() {// set up gameplay key bindingsSuper::SetupInputComponent();//LeftMouse 和 RightMouse 就是等下要在Editor中綁定按鍵用的,InputComponent->BindAction("LeftMouse", IE_Pressed, this, &AMyPlayerCtrler::OnLeftMousePressed);InputComponent->BindAction("RightMouse", IE_Pressed, this, &AMyPlayerCtrler::OnRightMousePressed);//按A鍵,準備攻擊InputComponent->BindAction("ReadyAtk", IE_Pressed, this, &AMyPlayerCtrler::OnReadAtk); }//一下幾個方法只是部分代碼 void AMyPlayerCtrler::OnLeftMousePressed() {FVector2D pressPos;this->GetMousePosition(pressPos.X, pressPos.Y);UE_LOG(GameLogger, Warning, TEXT("--- AMyPlayerCtrler::OnLeftMousePressed, pos:%s"), *pressPos.ToString());bool isAtk = false;FVector WorldPosition(0.f);AActor* const HitActor = GetClickTarget(pressPos, WorldPosition);AMyChar* tarChar = Cast<AMyChar>(HitActor);if (mSelectedVec.Num() > 0){if (mIsReadyAtk){//如果有人就鎖定目標, 沒人就移動if (tarChar != nullptr){AtkTarget(tarChar);}else{MoveDestination(WorldPosition);}isAtk = true;mIsReadyAtk = false;}}if (!isAtk){TArray<AMyChar*> dstCharVec;if (tarChar != nullptr){dstCharVec.Add(tarChar);}SetSelected(dstCharVec);}}void AMyPlayerCtrler::OnRightMousePressed() {FVector2D pressPos;this->GetMousePosition(pressPos.X, pressPos.Y);UE_LOG(GameLogger, Warning, TEXT("--- AMyPlayerCtrler::OnLeftMousePressed, pos:%s"), *pressPos.ToString());FHitResult HitResult;this->GetHitResultAtScreenPosition(pressPos, CurrentClickTraceChannel, true, HitResult);if (HitResult.bBlockingHit){MoveDestination(HitResult.ImpactPoint);} }void AMyPlayerCtrler::OnReadAtk() {mIsReadyAtk = true; }2、Editor 中綁定按鍵事件 LeftMouse 和 RightMouse
3、這里是真相
LeftMouse 是選中對象,RightMouse是選中對象奔跑到目的點(gif錄制實在太大,只能截圖了)
應要求貼部分代碼
void AMyPlayerCtrler::MoveDestination(const FVector& DestLocation) {for (AMyChar* selChar : mSelectedVec){selChar->MoveToDst(nullptr, DestLocation, false);} } // class AMyChar : public ACharacter, public IBehavInterface, public IMyInputInterface //繼承了輸入操作的方法void IMyInputInterface::MoveToDst(AMyChar* _target, const FVector& _loc, bool _isShift /* = false */) {if (mOwnChar->GetCharacterMovement()->Velocity.Size() > 0.f){mOwnChar->GetController()->StopMovement();}UNavigationSystem* const NavSys = mOwnChar->GetWorld()->GetNavigationSystem();if (NavSys != nullptr){NavSys->SimpleMoveToLocation(mOwnChar->GetController(), _loc);} }總結
以上是生活随笔為你收集整理的ue4 玩家控制器APlayerController的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 女性如何理解男人的性需求? 男性的性表达
- 下一篇: 田园将芜胡不归