创建两个摄像机,一个放在场景中,另一个放在Cube上
摄像机的细节面板中约束宽高比(Constrain Aspect Ratio)属性勾选后可以让两个摄像头之间的切换更流畅,但这不是必须的
会用到#include "Kismet/GameplayStatics.h"
头文件,获取玩家控制器的方法在这个类中
h
// Fill out your copyright notice in the Description page of Project Settings.#pragma once#include "CoreMinimal.h"
#include "GameFramework/Actor.h"
#include ated.h"UCLASS()
class QUICKSTART_API ACameraDirector : public AActor
{GENERATED_BODY()public: // Sets default values for this actor's propertiesACameraDirector();//在编辑器中可以编辑UPROPERTY(EditAnywhere)AActor* CameraOne;UPROPERTY(EditAnywhere)AActor* CameraTwo;float TimeToNextCameraChange;
protected:// Called when the game starts or when spawnedvirtual void BeginPlay() override;public: // Called every framevirtual void Tick(float DeltaTime) override;};
cpp
// 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;}// Called when the game starts or when spawned
void ACameraDirector::BeginPlay()
{Super::BeginPlay();}// Called every frame
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 (OurPlayerController->GetViewTarget() != CameraOne && CameraOne != nullptr){//立刻切换到摄像机1OurPlayerController->SetViewTarget(CameraOne);}else if (OurPlayerController->GetViewTarget() != CameraTwo && CameraTwo != nullptr){//平滑的切换到摄像机2OurPlayerController->SetViewTargetWithBlend(CameraTwo,SmoothBlendTime);}}}
}
使用数组TArray
h
// Fill out your copyright notice in the Description page of Project Settings.#pragma once#include "CoreMinimal.h"
#include "GameFramework/Actor.h"
#include ated.h"UCLASS()
class QUICKSTART_API ACameraDirector : public AActor
{GENERATED_BODY()public: // Sets default values for this actor's propertiesACameraDirector();UPROPERTY(EditAnywhere)TArray<AActor*> CameraArr;float TimeToNextCameraChange;
private:
protected:// Called when the game starts or when spawnedvirtual void BeginPlay() override;public: // Called every framevirtual void Tick(float DeltaTime) override;};
cpp
// Fill out your copyright notice in the Description page of Project Settings.#include "CameraDirector.h"
#include "Kismet/GameplayStatics.h"static int iCurrentNum = 0;
// 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;}// Called when the game starts or when spawned
void ACameraDirector::BeginPlay()
{Super::BeginPlay();}// Called every frame
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){int32 iCameraCount = CameraArr.Num();if (iCurrentCameraNum < iCameraCount){if (CameraArr[iCurrentCameraNum] != nullptr){//平滑地混合到摄像机OurPlayerController->SetViewTargetWithBlend(CameraArr[iCurrentCameraNum], SmoothBlendTime);}iCurrentCameraNum++;}else{iCurrentCameraNum = 0;//立即切换到摄像机1OurPlayerController->SetViewTarget(CameraArr[iCurrentCameraNum]);}}}
}
再回到细节面板就可以发现多了个小加号,点击加号可以自由增加数组的大小
本文发布于:2024-01-31 16:24:57,感谢您对本站的认可!
本文链接:https://www.4u4v.net/it/170668949629839.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
留言与评论(共有 0 条评论) |