UE4學習筆記6th:在C++中控制相機視圖

那麼,還是新建C++類
這裏寫圖片描述

命名爲CameraDirector
這裏寫圖片描述

在CameraDirector.h中輸入:

UPROPERTY(EditAnywhere)
AActor*CameraOne;

UPROPERTY(EditAntwhere)
AActor*CameraTwo;

float TimeToNextCameraChange;

這裏寫圖片描述

在CameraDirector.cpp中輸入:

include “Kismet/GamePlayStatics.h”

在文件的最開始部分

接着,在ACameraDirector::Tick中添加:

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)
    {
        if ((OurPlayerController->GetViewTarget() != CameraOne) && (CameraOne != nullptr))
        {
            //立即切換到相機1
            OurPlayerController->SetViewTarget(CameraOne);
        }
        else if ((OurPlayerController->GetViewTarget() != CameraTwo) && (CameraTwo != nullptr))
        {
            //平滑的切換到相機2
            OurPlayerController->SetViewTargetWithBlend(CameraTwo, SmoothBlendTime);
        }
    }
}

這裏寫圖片描述
完成後按F5執行。

以上代碼可以在相機1與相機2之間進行切換,接下來需要在編輯器中進行一些設置。

1,獲取PlayerController,APlayerController* OurPlayerController = UGameplayStatics::GetPlayerController(this, 0);

2,獲取當前視窗對象,OurPlayerController->GetViewTarget()

3,設置平滑過渡到新的目標視窗,OurPlayerController->SetViewTargetWithBlend(CameraTwo, SmoothBlendTime);

4,設置新的目標視窗,OurPlayerController->SetViewTarget(CameraOne);

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章