头文件
// Fill out your copyright notice in the Description page of Project Settings.
#pragma once
#include "CoreMinimal.h"
#include "GameFramework/Actor.h"
#include "ChangeCamera.generated.h"
UCLASS()
class CPLUSSTUDY_API AChangeCamera : public AActor
{
GENERATED_BODY()
public:
// Sets default values for this actor's properties
AChangeCamera();
protected:
// Called when the game starts or when spawned
virtual void BeginPlay() override;
public:
// Called every frame
virtual void Tick(float DeltaTime) override;
float TimetoChangeCamera;
int B = 0;
UPROPERTY(EditAnywhere)
AActor* CameraOne;
UPROPERTY(EditAnywhere)
AActor* CameraTwo;
UPROPERTY(EditAnywhere)
TArray<AActor*> CameraS;
};
CPP
// Fill out your copyright notice in the Description page of Project Settings.
#include "ChangeCamera.h"
#include "Kismet/GameplayStatics.h"
// Sets default values
AChangeCamera::AChangeCamera()
{
// Set this actor to call Tick() every frame. You can turn this off to improve performance if you don't need it.
PrimaryActorTick.bCanEverTick = true;
}
// Called when the game starts or when spawned
void AChangeCamera::BeginPlay()
{
Super::BeginPlay();
}
// Called every frame
void AChangeCamera::Tick(float DeltaTime)
{
Super::Tick(DeltaTime);
const int A = CameraS.Num();
const float TimeBetweenCameraChanges = 2.0f;
const float SmoothBlendTime = 0.75f;
TimetoChangeCamera -= DeltaTime;
if (TimetoChangeCamera <= 0.0f)
{
TimetoChangeCamera += 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);
//}
if (B < A)
{
OurPlayerController->SetViewTargetWithBlend(CameraS[B], SmoothBlendTime);
B++;
}else
{
B = 0;
}
}
}
}