一、效果展示
二、操作步骤
-
创建两个摄像机,一个放在场景中,另一个放在Cube上
-
摄像机的细节面板中约束宽高比(Constrain Aspect Ratio)属性勾选后可以让两个摄像头之间的切换更流畅,但这不是必须的
-
会用到#include "Kismet/GameplayStatics.h"
头文件,获取玩家控制器的方法在这个类中
三、已知摄像机数量写法
h
// Fill out your copyright notice in the Description page of Project Settings.
#pragma once
#include "CoreMinimal.h"
#include "GameFramework/Actor.h"
#include "CameraDirector.generated.h"
UCLASS()
class QUICKSTART_API ACameraDirector : public AActor
{
GENERATED_BODY()
public:
// Sets default values for this actor's properties
ACameraDirector();
//在编辑器中可以编辑
UPROPERTY(EditAnywhere)
AActor* CameraOne;
UPROPERTY(EditAnywhere)
AActor* CameraTwo;
float TimeToNextCameraChange;
protected:
// Called when the game starts or when spawned
virtual void BeginPlay() override;
public:
// Called every frame
virtual void Tick(float DeltaTime) override;
};
cpp
// Fill out your copyright notice in the Description page of Project Settings.
#include "CameraDirector.h"
#include "Kismet/GameplayStatics.h"
// Sets default values
ACameraDirector::ACameraDirector()
{
// 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 ACameraDirector::BeginPlay()
{
Super::BeginPlay();
}
// Called every frame
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;
//查找本地玩家控制的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);
}
}
}
}
四、动态增加摄像机数量
使用数组TArray
h
// Fill out your copyright notice in the Description page of Project Settings.
#pragma once
#include "CoreMinimal.h"
#include "GameFramework/Actor.h"
#include "CameraDirector.generated.h"
UCLASS()
class QUICKSTART_API ACameraDirector : public AActor
{
GENERATED_BODY()
public:
// Sets default values for this actor's properties
ACameraDirector();
UPROPERTY(EditAnywhere)
TArray<AActor*> CameraArr;
float TimeToNextCameraChange;
private:
protected:
// Called when the game starts or when spawned
virtual void BeginPlay() override;
public:
// Called every frame
virtual void Tick(float DeltaTime) override;
};
cpp
// Fill out your copyright notice in the Description page of Project Settings.
#include "CameraDirector.h"
#include "Kismet/GameplayStatics.h"
static int iCurrentNum = 0;
// Sets default values
ACameraDirector::ACameraDirector()
{
// 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 ACameraDirector::BeginPlay()
{
Super::BeginPlay();
}
// Called every frame
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;
//查找本地玩家控制的actor
APlayerController* OurPlayerController = UGameplayStatics::GetPlayerController(this, 0);
if (OurPlayerController)
{
int32 iCameraCount = CameraArr.Num();
if (iCurrentCameraNum < iCameraCount)
{
if (CameraArr[iCurrentCameraNum] != nullptr)
{
//平滑地混合到摄像机
OurPlayerController->SetViewTargetWithBlend(CameraArr[iCurrentCameraNum], SmoothBlendTime);
}
iCurrentCameraNum++;
}
else
{
iCurrentCameraNum = 0;
//立即切换到摄像机1
OurPlayerController->SetViewTarget(CameraArr[iCurrentCameraNum]);
}
}
}
}
再回到细节面板就可以发现多了个小加号,点击加号可以自由增加数组的大小