<L3>c++两个摄像机的平滑切换

459 阅读1分钟

c++两个摄像机的平滑切换

1.在场景中创建两个camera,并创建c++类CameraDirector

2.CameraDirector.h

	UPROPERTY(EditAnywhere)
		AActor* CameraOne;

	UPROPERTY(EditAnywhere)
		AActor* CameraTwo;

	float TimeToNextCameraChange;
	
	
	//本变量没有被使用 是数组形式的尝试
	UPROPERTY(EditAnywhere)
		AActor* CameraArr[5];

3.CameraDirector.cpp

// 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)
		{
			//GetViewTarget() 获取controller正在看的actor
			if ((OurPlayerController->GetViewTarget() != CameraOne) && (CameraOne != nullptr))
			{
				// 立即切换到摄像机1。
				OurPlayerController->SetViewTarget(CameraOne);
			}
			else if ((OurPlayerController->GetViewTarget() != CameraTwo) && (CameraTwo != nullptr))
			{
				// 平滑地混合到摄像机2。
				OurPlayerController->SetViewTargetWithBlend(CameraTwo, SmoothBlendTime);
			}
		}
	}
}