Camera switching

 

Camera switching is also an important content,

These two cameras from different sources, drag the camera to direct a scene, a cube is to first drag it to the scene, plus a camera assembly. The latter should be attached to any object, such as lights, etc.

 

Natural-based cameras have cameraDirector, manage, one immediately switched, a smooth transition is

   Are ourPlayerController-> SetViewTarget (_cameraOne);

ourPlayerController->SetViewTargetWithBlend(_cameraTwo, _smoothBlendTime);

 

Then put the camera class drag scene, the strange thing is cameraDirector become cameraDirector1, but this harmless. Then put two cameras associated member variables to the scene of two cameras ok.

 

Note that local playerController is 0
    // Find the handling of local players control the Actor
    APlayerController * ourPlayerController = UGameplayStatics :: GetPlayerController (the this, 0);

UE4 editor position is

GamePlayActors-> player start-> Rating -> automatically receive input, you will find and disable different Player

Deep into the code, I found to be a process of exhaustive searching


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;
}

 

At last,

Some of the code on the document unsatisfactory, especially with the following:

head File:

// 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();

protected:
    // Called when the game starts or when spawned
    virtual void BeginPlay() override;

public:    
    // Called every frame
    virtual void Tick(float DeltaTime) override;

public:
    UPROPERTY (EditAnywhere)
        AActor * _cameraOne;
    UPROPERTY (EditAnywhere)
        AActor * _cameraTwo;
    // camera switching time count
    a float _timeToNextCameraChange;
    // camera residence time of
    a float _timeBetweenCameraChanges;
    // smooth transition time
    float _smoothBlendTime;

};
Implementation file

// 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;
    
    _timeToNextCameraChange = 1.0;

    // camera dwell time
    _timeBetweenCameraChanges = 2.0f;
    // smooth transition time
    _smoothBlendTime = 0.75f;
}

// Called when the game starts or when spawned
void AcameraDirector::BeginPlay()
{
    Super::BeginPlay();
    
}

// Called every frame
void AcameraDirector::Tick(float deltaTime)
{
    Super::Tick(deltaTime);

    // decrement value per frame, or if greater than 0, the process returns
    _timeToNextCameraChange - = deltaTime;

    IF (_timeToNextCameraChange> 0.0f)
    {
        return;
    }
    // if less than 0, add a value, the camera converts
    _timeToNextCameraChange + = _timeBetweenCameraChanges;

    // search process the local player control the Actor
    APlayerController UGameplayStatics :: = * ourPlayerController GetPlayerController (the this, 0);
    IF (ourPlayerController == NULL)
    {
        return;
    }
    
    AActor currentActor * = ourPlayerController-> GetViewTarget ();

    // If not, the camera 1 and the camera 1 is not empty, then immediately switch to the camera 1
    IF ((currentActor = _cameraOne) && (cameraOne _ = NULL)!!)
    {
        OurPlayerController-> SetViewTarget (_cameraOne);
    }
    // if not camera 2, and camera 2 is not empty, then immediately switch to the camera 2
    the else IF ((currentActor = _cameraTwo) && (_cameraTwo = NULL)!!)
    {
        ourPlayerController-> SetViewTargetWithBlend (_cameraTwo, _smoothBlendTime);
    }

}


 

 

 

Published 673 original articles · won praise 18 · views 280 000 +

Guess you like

Origin blog.csdn.net/directx3d_beginner/article/details/105134664