在类的头文件里添加
#include "Kismet/GameplayStatics.h"
Tick中的关键代码如下:
TimeToNextCameraChange -= DeltaTime;
if (TimeToNextCameraChange <= 0 )
{
TimeToNextCameraChange += TimeIntervalBetweenCameraChange;
APlayerController* player = UGameplayStatics::GetPlayerController(this, 0);
if (CameraIndex > CameraList.Num() - 1)
CameraIndex = 0;
if (player)
{
if ((CameraList[CameraIndex] != nullptr) && (player->GetViewTarget() != CameraList[CameraIndex]))
{
player->SetViewTargetWithBlend(CameraList[CameraIndex], SmoothBlendTime);
}
}
CameraIndex++;
}
每隔TimeIntervalBetwweenCameraChange个时间段,就在列表中找下一个相机,并把玩家的视角摄像机设置成该相机。
要使用UGameplayStatics必须先正确加载头文件