UE5 游戲模板 —— TopDownGame 俯視角游戲
- 前言
- 一、模塊導入
- 二、TopDownGameMode
- 三、TopDownPlayerController
- 1、構造函數
- 2、SetupInputComponent
- 初始化新輸入系統
- 處理輸入邏輯
- 四、TopDownCharacter
- 五、射線檢測
- 總結
前言
上一篇文章介紹了一下PuzzleGame模板的流程,我們循序漸進這次介紹TopDownGame。
實際上TopDownGame和之前的PuzzleGame非常類似都是俯視角都要使用射線檢測去做相應的判定。但是TopDownGame提供了角色尋路和角色位移的方式。
一、模塊導入
UE是以導入模塊來構建項目的,我們可以自己定義插件等內容當作一個模塊來進行導入。
可以看到下圖中導入了很多的模塊
其中 “Core”, “CoreUObject”, “Engine”, “InputCore” 是常用的也是默認都會導入的幾個模塊,有著最基礎的內容。
“AIModule” 這個是用作尋路的必要模塊
“Niagara” 粒子系統相關的模塊
“EnhancedInput” 新的輸入系統模塊
二、TopDownGameMode
我們在之前的PuzzleGame中就有講解過,下面的方式可以指定C++的藍圖派生類
三、TopDownPlayerController
1、構造函數
還是和上一節的內容相似,都是顯示鼠標和設置鼠標的默認類型。
CachedDestination用于記錄點擊到的點
FollowTime用于記錄鼠標按住的時間
2、SetupInputComponent
初始化新輸入系統
獲取新輸入的本地玩家子系統,對子系統設置MappingContext
// Add Input Mapping Contextif (UEnhancedInputLocalPlayerSubsystem* Subsystem = ULocalPlayer::GetSubsystem<UEnhancedInputLocalPlayerSubsystem>(GetLocalPlayer())){Subsystem->AddMappingContext(DefaultMappingContext, 0);}
將InputComponent 轉換成新輸入組件并綁定事件
// Set up action bindings
if (UEnhancedInputComponent* EnhancedInputComponent = Cast<UEnhancedInputComponent>(InputComponent))
{// Setup mouse input eventsEnhancedInputComponent->BindAction(SetDestinationClickAction, ETriggerEvent::Started, this, &ATestTopDownPlayerController::OnInputStarted);EnhancedInputComponent->BindAction(SetDestinationClickAction, ETriggerEvent::Triggered, this, &ATestTopDownPlayerController::OnSetDestinationTriggered);EnhancedInputComponent->BindAction(SetDestinationClickAction, ETriggerEvent::Completed, this, &ATestTopDownPlayerController::OnSetDestinationReleased);EnhancedInputComponent->BindAction(SetDestinationClickAction, ETriggerEvent::Canceled, this, &ATestTopDownPlayerController::OnSetDestinationReleased);// Setup touch input eventsEnhancedInputComponent->BindAction(SetDestinationTouchAction, ETriggerEvent::Started, this, &ATestTopDownPlayerController::OnInputStarted);EnhancedInputComponent->BindAction(SetDestinationTouchAction, ETriggerEvent::Triggered, this, &ATestTopDownPlayerController::OnTouchTriggered);EnhancedInputComponent->BindAction(SetDestinationTouchAction, ETriggerEvent::Completed, this, &ATestTopDownPlayerController::OnTouchReleased);EnhancedInputComponent->BindAction(SetDestinationTouchAction, ETriggerEvent::Canceled, this, &ATestTopDownPlayerController::OnTouchReleased);
}
處理輸入邏輯
點擊鼠標左鍵開始時先停止角色移動
void ATestTopDownPlayerController::OnInputStarted()
{StopMovement();
}
按住觸發鼠標左鍵
1.先累加幀時間間隔
2.發射射線(可能會疑問這個發射射線的方法,其實是對之前PuzzleGame中發射射線方法的一個封裝下面詳細講解一下這個地方)
3.記錄射線碰撞的點
4.計算移動的方向向量
void ATestTopDownPlayerController::OnSetDestinationTriggered()
{// We flag that the input is being pressedFollowTime += GetWorld()->GetDeltaSeconds();// We look for the location in the world where the player has pressed the inputFHitResult Hit;bool bHitSuccessful = false;if (bIsTouch){bHitSuccessful = GetHitResultUnderFinger(ETouchIndex::Touch1, ECollisionChannel::ECC_Visibility, true, Hit);}else{bHitSuccessful = GetHitResultUnderCursor(ECollisionChannel::ECC_Visibility, true, Hit);}// If we hit a surface, cache the locationif (bHitSuccessful){CachedDestination = Hit.Location;}// Move towards mouse pointer or touchAPawn* ControlledPawn = GetPawn();if (ControlledPawn != nullptr){FVector WorldDirection = (CachedDestination - ControlledPawn->GetActorLocation()).GetSafeNormal();ControlledPawn->AddMovementInput(WorldDirection, 1.0, false);}
}
松開鼠標左鍵
如果按鍵按下的時長小于設定值判定是鼠標瞬間點擊,將玩家使用導航移動到目標點,同時播放粒子特效和聲音
void ATestTopDownPlayerController::OnSetDestinationReleased()
{// If it was a short pressif (FollowTime <= ShortPressThreshold){// We move there and spawn some particlesUAIBlueprintHelperLibrary::SimpleMoveToLocation(this, CachedDestination);UNiagaraFunctionLibrary::SpawnSystemAtLocation(this, FXCursor, CachedDestination, FRotator::ZeroRotator, FVector(1.f, 1.f, 1.f), true, true, ENCPoolMethod::None, true);if (AudioSound != nullptr){UGameplayStatics::PlaySoundAtLocation(GetWorld(), AudioSound, CachedDestination);}}FollowTime = 0.f;
}
四、TopDownCharacter
初始化相機和相機臂
五、射線檢測
我們已經遇到了多次射線檢測讓我們來仔細看一下
首先無論是哪種射線檢測函數本質上是從一個點到另一個點的連線在此連線的路徑上是否有物體遮擋碰撞。
1.我們要先獲取到鼠標在屏幕上面的坐標
2.將屏幕上的坐標轉換為世界中的坐標,可以想象是攝像機的那個投影面的位置
3.需要知道方向,其實就是攝像機看向的方向
4.確定終點,起點+方向*長度
5.傳遞需要碰撞的參數,調用LineTraceSingleByChannel
在PuzzleGame中我們曾用過這個函數,獲取到了世界的位置和方向,實際上就是獲取屏幕鼠標位置讓后通過矩陣轉換得到最終的值
我們現在在來看一下TopDownGame的射線檢測
其實本質的原理都是一樣的
總結
以上就是今天要講的內容,自此兩個典型的俯視角游戲就介紹到這里,接下來是第一人稱和第三人稱射擊游戲的模板。