游戏控制多个摄像机的切换
一、两个摄像机的切换 官方文档 二、多个摄像机的切换 代码实现
#include "CoreMinimal.h"
#include "GameFramework/Actor.h"
#include "CameraDirector.generated.h"
UCLASS()
class FLOATOBJECT_API ACameraDirector : public AActor
{
GENERATED_BODY()
public:
ACameraDirector();
protected:
virtual void BeginPlay() override;
public:
virtual void Tick(float DeltaTime) override;
float TimeToNextCameraChange;
UPROPERTY(EditAnywhere)
AActor* Cameras[6];
};
#include "CameraDirector.h"
#include "Kismet/GameplayStatics.h"
int i = 0;
ACameraDirector::ACameraDirector()
{
PrimaryActorTick.bCanEverTick = true;
}
void ACameraDirector::BeginPlay()
{
Super::BeginPlay();
}
void ACameraDirector::Tick(float DeltaTime)
{
Super::Tick(DeltaTime);
const float TimeBetweenCameraChanges = 2.0f;
const float SmoothBlendTime = 0.75f;
TimeToNextCameraChange -= DeltaTime;
if (TimeToNextCameraChange <= 0.0f)
{
TimeToNextCameraChange += TimeBetweenCameraChanges;
APlayerController* OurPlayerController = UGameplayStatics::GetPlayerController(this, 0);
if (OurPlayerController)
{
while ((OurPlayerController->GetViewTarget() != Cameras[i])&& (Cameras[i] != nullptr)&&i<6)
{
OurPlayerController->SetViewTargetWithBlend(Cameras[i], SmoothBlendTime);
i++;
break;
}
}
}
if (i == 6)
i = 0;
}
|