那麼,還是新建C++類
命名爲CameraDirector
在CameraDirector.h中輸入:
UPROPERTY(EditAnywhere)
AActor*CameraOne;
UPROPERTY(EditAntwhere)
AActor*CameraTwo;
float TimeToNextCameraChange;
在CameraDirector.cpp中輸入:
include “Kismet/GamePlayStatics.h”
在文件的最開始部分
接着,在ACameraDirector::Tick中添加:
const float TimeBetweenCameraChanges = 2.0f;
const float SmoothBlendTime = 0.75f;
TimeToNextCameraChange -= DeltaTime;
if (TimeToNextCameraChange <= 0.0f)
{
TimeToNextCameraChange += TimeBetweenCameraChanges;
//搜尋處理玩家控制的Actor
APlayerController* OurPlayerController = UGameplayStatics::GetPlayerController(this, 0);
if (OurPlayerController)
{
if ((OurPlayerController->GetViewTarget() != CameraOne) && (CameraOne != nullptr))
{
//立即切換到相機1
OurPlayerController->SetViewTarget(CameraOne);
}
else if ((OurPlayerController->GetViewTarget() != CameraTwo) && (CameraTwo != nullptr))
{
//平滑的切換到相機2
OurPlayerController->SetViewTargetWithBlend(CameraTwo, SmoothBlendTime);
}
}
}
完成後按F5執行。
以上代碼可以在相機1與相機2之間進行切換,接下來需要在編輯器中進行一些設置。
1,獲取PlayerController,APlayerController* OurPlayerController = UGameplayStatics::GetPlayerController(this, 0);
2,獲取當前視窗對象,OurPlayerController->GetViewTarget()
3,設置平滑過渡到新的目標視窗,OurPlayerController->SetViewTargetWithBlend(CameraTwo, SmoothBlendTime);
4,設置新的目標視窗,OurPlayerController->SetViewTarget(CameraOne);