01 CamerDirector.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 HOWTO_AUTOCAMERA_API ACameraDirector : public AActor
{
GENERATED_BODY()
public:
// Sets default values for this actor's properties
// 为此Actor的属性设置默认值
ACameraDirector();
protected:
// Called when the game starts or when spawned
// 当游戏开始或生成时调用
virtual void BeginPlay() override;
public:
// Called every frame
// 每一帧调用
virtual void Tick(float DeltaTime) override;
//值将不会被重置
//允许我们在 虚幻编辑器 中设置摄像机1(CameraOne)
UPROPERTY(EditAnywhere)
AActor* CameraOne;
//允许我们在 虚幻编辑器 中设置摄像机2(CameraTwo)
UPROPERTY(EditAnywhere)
AActor* CameraTwo;
float TimeToNextCameraChange;
};
01 01 转圈圈
//允许我们在 虚幻编辑器 中设置摄像机1(CameraOne)
UPROPERTY(EditAnywhere)
AActor* CameraOne;
//允许我们在 虚幻编辑器 中设置摄像机2(CameraTwo)
UPROPERTY(EditAnywhere)
AActor* CameraTwo;
演示
02 CameraDirector.cpp
// Fill out your copyright notice in the Description page of Project Settings.
#include "HowTo_AutoCamera.h"
#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);
//以下加的
//每隔3秒在两个不同的摄像机间切换默认玩家的视图、
//摄像机停留时间
const float TimeBetweenCameraChanges = 2.0f;
//摄像机平滑过渡的时间
const float SmoothBlendTime = 0.75f;
TimeToNextCameraChange -= DeltaTime;
if (TimeToNextCameraChange <= 0.0f)
{
TimeToNextCameraChange += TimeBetweenCameraChanges;
/*
指针APlayerController*(世界中一个物体引用,玩家索引)
class APlayerController* UGameplayStatics::GetPlayerController(const UObject* WorldContextObject, int32 PlayerIndex )
{
if (UWorld* World = GEngine->GetWorldFromContextObject(WorldContextObject, EGetWorldErrorMode::LogAndReturnNull))
{
uint32 Index = 0;
for (FConstPlayerControllerIterator Iterator = World->GetPlayerControllerIterator(); Iterator; ++Iterator)
{
APlayerController* PlayerController = Iterator->Get();
if (Index == PlayerIndex)
{
return PlayerController;
}
Index++;
}
}
return nullptr;
}
*/
// 查找处理本地玩家控制的actor
//好比是手柄、键盘
APlayerController* OurPlayerController = UGameplayStatics::GetPlayerController(this, 0);
if (OurPlayerController)
{
/*
AActor*指针
AActor* APlayerController::GetViewTarget() const
{
return PlayerCameraManager ? PlayerCameraManager->GetViewTarget() : NULL;
}
*/
//获取玩家控制的Camera,不是CameraOne就切回CameraOne
if ((OurPlayerController->GetViewTarget() != CameraOne) && (CameraOne != nullptr))
{
// 立即切换到摄像机1。
OurPlayerController->SetViewTarget(CameraOne);
}
//不是CameraTwo
else if ((OurPlayerController->GetViewTarget() != CameraTwo) && (CameraTwo != nullptr))
{
// 平滑地切换到CameraTwo
//blend,混合
OurPlayerController->SetViewTargetWithBlend(CameraTwo, SmoothBlendTime);
}
}
}
}
02 01 PlayerController
// 查找处理本地玩家控制的actor
//好比是手柄、键盘
APlayerController* OurPlayerController = UGameplayStatics::GetPlayerController(this, 0);