UE4 场景中两个摄像头之间的切换

我们在场景中放置两个摄像头,每隔一段时间就在两个摄像头之间进行视角切换。

  1. 新建一个项目空项目,新键一个空关卡。在关卡中放置两个摄像头Actor,一个就是单独的摄像头组件,另外一个是放在Actor上的摄像头.
    在这里插入图片描述
  2. 新建一个摄像头的Actor类,有两个Actor成员变量,用来保存两个相机
// 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 CHANGECAMERA_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;

};

// 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 = 5.0f;
	//平滑过渡时间
	const float SmoothBlendTime = /*0.75*/2.0f;
	TimeToNextCameraChange -= DeltaTime;
	if (TimeToNextCameraChange <= 0.0f) {
    
    
		TimeToNextCameraChange += TimeBetweenCameraChanges;

		//获取本机玩家控制器Player0
		APlayerController* OurPlayerController = UGameplayStatics::GetPlayerController(this, 0);
		if (OurPlayerController) {
    
    

			//GetViewTarget 获取玩家身上的相机Actor
			//如果不是摄像机one的话,就直接切花到摄像机one
			if ((OurPlayerController->GetViewTarget() != CameraOne) && (CameraOne != nullptr)) {
    
    
				//OurPlayerController->SetViewTarget(CameraOne);
				OurPlayerController->SetViewTargetWithBlend(CameraOne, SmoothBlendTime);
			} else if ((OurPlayerController->GetViewTarget() != CameraTwo) && (CameraTwo != nullptr)) {
    
    
				//平滑的切换到摄像机two
				OurPlayerController->SetViewTargetWithBlend(CameraTwo, SmoothBlendTime, EViewTargetBlendFunction::VTBlend_Linear);
			}
		}
	}
}
  1. 然后把生成的类放置到场景中,并且给两个成员变量选定场景中的两个摄像头
    在这里插入图片描述
    然后运行就可以看到场景在两个摄像头之间来回切换。

aaa

猜你喜欢

转载自blog.csdn.net/wb175208/article/details/127950846
今日推荐