C++ smooth switch between two cameras

1. Create two cameras in the scene and create the c++ class CameraDirector

2.CameraDirector.h

	UPROPERTY(EditAnywhere)
		AActor* CameraOne;

	UPROPERTY(EditAnywhere)
		AActor* CameraTwo;

	float TimeToNextCameraChange;
	
	
	// This variable is not being used as an array attempt
	UPROPERTY(EditAnywhere)
		AActor* CameraArr[5];
Copy the code

3.CameraDirector.cpp

// Called every frame
void ACameraDirector::Tick(float DeltaTime)
{
	Super::Tick(DeltaTime);

	const float TimeBetweenCameraChanges = 2.0 f;
	const float SmoothBlendTime = 0.75 f;
	TimeToNextCameraChange -= DeltaTime;
	if (TimeToNextCameraChange <= 0.0 f)
	{
		TimeToNextCameraChange += TimeBetweenCameraChanges;

		// Find the actor that handles local player control.
		APlayerController* OurPlayerController = UGameplayStatics::GetPlayerController(this.0);
		if (OurPlayerController)
		{
			//GetViewTarget() gets the actor the controller is looking at
			if ((OurPlayerController->GetViewTarget()! = CameraOne) && (CameraOne ! =nullptr))
			{
				// Switch to camera 1 immediately.
				OurPlayerController->SetViewTarget(CameraOne);
			}
			else if ((OurPlayerController->GetViewTarget()! = CameraTwo) && (CameraTwo ! =nullptr))
			{
				// Blend smoothly into camera 2.
				OurPlayerController->SetViewTargetWithBlend(CameraTwo, SmoothBlendTime); }}}}Copy the code