ue4 玩家控制器APlayerController
生活随笔
收集整理的這篇文章主要介紹了
ue4 玩家控制器APlayerController
小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
通過(guò) APlayerController 可以讓輸入設(shè)備控制游戲,可以直接在藍(lán)圖中設(shè)置,不過(guò)我比較喜歡折騰c++(邏輯代碼都是用c++寫(xiě))
1、創(chuàng)建一個(gè) AMyPlayerCtrler 繼承自 APlayerController,重寫(xiě)一些方法
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鍵,準(zhǔn)備攻擊InputComponent->BindAction("ReadyAtk", IE_Pressed, this, &AMyPlayerCtrler::OnReadAtk); }//一下幾個(gè)方法只是部分代碼 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){//如果有人就鎖定目標(biāo), 沒(méi)人就移動(dòng)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 是選中對(duì)象,RightMouse是選中對(duì)象奔跑到目的點(diǎn)(gif錄制實(shí)在太大,只能截圖了)
應(yīng)要求貼部分代碼
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);} }總結(jié)
以上是生活随笔為你收集整理的ue4 玩家控制器APlayerController的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: 女性如何理解男人的性需求? 男性的性表达
- 下一篇: 田园将芜胡不归