UnrealEngine学习笔记11:游戏控制的摄像机

https://docs.unrealengine.com/zh-CN/Programming/Tutorials/AutoCamera/index.html

MyPawn.h

// 版权所有 1998-2017 Epic Games, Inc。保留所有权利。

#pragma once

#include "GameFramework/Actor.h"
#include "CameraDirector.generated.h"

UCLASS()
class HOWTO_AUTOCAMERA_API ACameraDirector : public AActor
{
    GENERATED_BODY()

public: 
    // 为此Actor的属性设置默认值
    ACameraDirector();

protected:
    // 当游戏开始或生成时调用
    virtual void BeginPlay() override;

public:
    // 每一帧调用
    virtual void Tick( float DeltaSeconds ) override;

    UPROPERTY(EditAnywhere)
    AActor* CameraOne;

    UPROPERTY(EditAnywhere)
    AActor* CameraTwo;

    float TimeToNextCameraChange;
};

MyPawn.cpp

// 版权所有 1998-2017 Epic Games, Inc。保留所有权利。

#include "HowTo_AutoCamera.h"
#include "CameraDirector.h"
#include "Kismet/GameplayStatics.h"

// 设置默认值
ACameraDirector::ACameraDirector()
{
    // 将此Actor设置为每一帧调用Tick()。如果不需要,可以关闭此选项来提高性能。
    PrimaryActorTick.bCanEverTick = true;

}

// 当游戏开始或生成时调用
void ACameraDirector::BeginPlay()
{
    Super::BeginPlay();

}

// 每一帧调用
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)
        {
            if (CameraTwo && (OurPlayerController->GetViewTarget() == CameraOne))
            {
                //平滑地混合到摄像机2。
                OurPlayerController->SetViewTargetWithBlend(CameraTwo, SmoothBlendTime);
            }
            else if (CameraOne)
            {
                //立即切换到摄像机1。
                OurPlayerController->SetViewTarget(CameraOne);
            }
        }
    }
}

UPROPERTY的作用:属性说明符

https://docs.unrealengine.com/zh-CN/Programming/UnrealArchitecture/Reference/Properties/Specifiers/index.html

发布了337 篇原创文章 · 获赞 236 · 访问量 1万+

猜你喜欢

转载自blog.csdn.net/weixin_42721412/article/details/105557097