摄像机切换

 

摄像机切换也是个重要内容,

这两个摄像机来源不同,一个是直接拖动摄像机到场景,一个是先把cube拖动到场景,再加个摄像机组件。后者应该是附着在任何物体上,比如车灯等

 

自然要有个摄像机类cameraDirector,进行管理,一个是立即切换,一个是平滑过渡

   分别是ourPlayerController->SetViewTarget(_cameraOne);

ourPlayerController->SetViewTargetWithBlend(_cameraTwo, _smoothBlendTime);

 

然后再把摄像机类拖到场景,奇怪的是cameraDirector变成cameraDirector1了,不过这个无伤大雅。然后再把两个摄像机成员变量关联到场景中的两个摄像机就ok了.

 

需要注意的是playerController本地是0
    //查找处理本地玩家控制的actor
    APlayerController* ourPlayerController = UGameplayStatics::GetPlayerController(this, 0);

UE4编辑器的位置是

GamePlayActors->player start->评级->自动接收输入,就会发现禁用和不同的Player

深入代码,发现是个遍历查找的过程


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

 

最后,

文档上的代码有些不尽人意,特更改如下:

头文件:

// 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;
    //切换摄像机时间计数
    float _timeToNextCameraChange;
    //摄像机停留时间
    float _timeBetweenCameraChanges;
    //平滑过渡时间
    float _smoothBlendTime;

};
实现文件

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

    //摄像机停留时间
    _timeBetweenCameraChanges = 2.0f;
    //平滑过渡时间
    _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);

    //每帧减少值,如果还是大于0,则返回
    _timeToNextCameraChange -= deltaTime;

    if (_timeToNextCameraChange > 0.0f)
    {
        return;
    }
    //如果小于0,则加上某个值,进行转换摄像机
    _timeToNextCameraChange += _timeBetweenCameraChanges;

    //查找处理本地玩家控制的actor
    APlayerController* ourPlayerController = UGameplayStatics::GetPlayerController(this, 0);
    if (NULL == ourPlayerController)
    {
        return;
    }
    
    AActor* currentActor = ourPlayerController->GetViewTarget();

    //如果不是摄像机1,且摄像机1不为空,则立即切换到摄像机1
    if ( (currentActor != _cameraOne) && ( NULL  !=_cameraOne ))
    {
        ourPlayerController->SetViewTarget(_cameraOne);
    }
    //如果不是摄像机2,且摄像机2不为空,则立即切换到摄像机2
    else if ((currentActor != _cameraTwo) && (NULL != _cameraTwo))
    {
        ourPlayerController->SetViewTargetWithBlend(_cameraTwo, _smoothBlendTime);
    }

}


 

 

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值