C++ クラスウィザード を起動し、[All Classes (すべてのクラス)] のチェックボックスを有効にしてから、検索フィールドに「CameraActor」と入力して、Camera Actor クラスを選択して ExampleCameraActor という名前で新しく作成します。
[C++ Class (C++ クラス)] パネルで、ExampleCamera を右クリックし、[C++ Class] アクション メニューのドロップダウンから、[Create a Blueprint class based on ExampleCameraActor (ExampleCameraActor に基づいてブループリントクラスを作成)] を選択します。次に、BP_ExampleCameraActor のインスタンスをレベルにドラッグします。
protected:
//Collision Bounds of the Actor Volume
UPROPERTY(EditAnywhere, BlueprintReadWrite)
class UBoxComponent* OverlapVolume;
//Camera Actor which the Actor Volume blends to
UPROPERTY(EditAnywhere, BlueprintReadWrite)
TSubclassOf<ACameraActor> CameraToFind;
//Blend time for camera transition
UPROPERTY(EditAnywhere, BlueprintReadWrite)
float CameraBlendTime;
//Begin and End Overlap methods for our OverlapVolume Actor
virtual void NotifyActorBeginOverlap(AActor* OtherActor);
virtual void NotifyActorEndOverlap(AActor* OtherActor);
void ABlendTriggerVolume::NotifyActorBeginOverlap(AActor* OtherActor){
//Cast check to see if overlapped Actor is Third Person Player Character
if (AStaticCamerasCharacter* PlayerCharacterCheck = Cast<AStaticCamerasCharacter>(OtherActor))
{
//Cast to Player Character's PlayerController
if (APlayerController* PlayerCharacterController = Cast<APlayerController>(PlayerCharacterCheck->GetController()))
{
//Array to contain found Camera Actors
TArray<AActor*> FoundActors;
//Utility function to populate array with all Camera Actors in the level
UGameplayStatics::GetAllActorsOfClass(GetWorld(), CameraToFind, FoundActors);
//Sets Player Controller view to the first CameraActor found
PlayerCharacterController->SetViewTargetWithBlend(FoundActors[0], CameraBlendTime, EViewTargetBlendFunction::VTBlend_Linear);
}
}
}
void ABlendTriggerVolume::NotifyActorEndOverlap(AActor* OtherActor){
if (AStaticCamerasCharacter* PlayerCharacterCheck = Cast<AStaticCamerasCharacter>(OtherActor))
{
if (APlayerController* PlayerCharacterController = Cast<APlayerController>(PlayerCharacterCheck->GetController()))
{
//Blend to Player Character's Camera Component.
PlayerCharacterController->SetViewTargetWithBlend(PlayerCharacterController->GetPawn(), CameraBlendTime, EViewTargetBlendFunction::VTBlend_Linear);
}
}
}
コードを コンパイル します。
完成コード
BlendTriggerVolume.h
#pragma once
#include "CoreMinimal.h"
#include "GameFramework/Actor.h"
#include "BlendTriggerVolume.generated.h"
UCLASS()
class STATICCAMERAS_API ABlendTriggerVolume : public AActor
{
GENERATED_BODY()
public:
// Sets default values for this actor's properties
ABlendTriggerVolume();
protected:
//Collision Bounds of the Actor Volume
UPROPERTY(EditAnywhere, BlueprintReadWrite)
class UBoxComponent* OverlapVolume;
//Camera Actor which the Actor Volume blends to
UPROPERTY(EditAnywhere, BlueprintReadWrite)
TSubclassOf<ACameraActor> CameraToFind;
//Blend time for camera transition
UPROPERTY(EditAnywhere, BlueprintReadWrite, meta =(ClampMin = 0.0f ))
float CameraBlendTime;
//Begin and End Overlap Actor methods for our OverlapVolume Actor.
//Begin and End Overlap methods for our OverlapVolume Actor
virtual void NotifyActorBeginOverlap(AActor* OtherActor);
virtual void NotifyActorEndOverlap(AActor* OtherActor);
};
BlendTriggerVolume.cpp
#include "BlendTriggerVolume.h"
#include "Components/BoxComponent.h"
#include "StaticCamerasCharacter.h"
#include "Camera/CameraActor.h"
#include "Runtime/Engine/Classes/Kismet/GameplayStatics.h"
// Sets default values
ABlendTriggerVolume::ABlendTriggerVolume()
{
//Create box component default components
OverlapVolume = CreateDefaultSubobject<UBoxComponent>(TEXT("CameraProximityVolume"));
//Set the box component attachment to the root component.OverlapVolume->SetupAttachment(RootComponent);
//Set the CameraBlendTime
CameraBlendTime = 1.0f;
}
// Called when the game starts or when spawned
void ABlendTriggerVolume::BeginPlay()
{
Super::BeginPlay();
}
void ABlendTriggerVolume::NotifyActorBeginOverlap(AActor * OtherActor)
{
//Cast check to see if overlapped Actor is Third Person Player Character
if (AStaticCamerasCharacter* PlayerCharacterCheck = Cast<AStaticCamerasCharacter>(OtherActor))
{
//Cast to Player Character's PlayerController
if (APlayerController* PlayerCharacterController = Cast<APlayerController>(PlayerCharacterCheck->GetController()))
{
//Array to contain found Camera Actors
TArray<AActor*> FoundActors;
//Utility function to populate array with all Camera Actors in the level
UGameplayStatics::GetAllActorsOfClass(GetWorld(), CameraToFind, FoundActors);
//Sets Player Controller view to the first CameraActor found PlayerCharacterController->SetViewTargetWithBlend(FoundActors[0], CameraBlendTime, EViewTargetBlendFunction::VTBlend_Linear);
}
}
}
void ABlendTriggerVolume::NotifyActorEndOverlap(AActor* OtherActor)
{
if (AStaticCamerasCharacter* PlayerCharacterCheck = Cast<AStaticCamerasCharacter>(OtherActor))
{
if (APlayerController* PlayerCharacterController = Cast<APlayerController>(PlayerCharacterCheck->GetController()))
{
//Blend to Player Character's Camera Component.
PlayerCharacterController->SetViewTargetWithBlend(PlayerCharacterController->GetPawn(), CameraBlendTime, EViewTargetBlendFunction::VTBlend_Linear);
}
}
}
// Called every frame
void ABlendTriggerVolume::Tick(float DeltaTime)
{
Super::Tick(DeltaTime);
}
オーバーラップ トリガー アクタを設定する
オーバーラップ アクタを作成したので、それをレベルに配置し、境界を設定する必要があります。
まず、「C++ Classes」フォルダに移動し、BlendTriggerVolume クラスを右クリックして、[Create Blueprint Class based on BlendTriggerVolume ] を選択し、ブループリント アクタ に「BP_BlendTriggerVolume」という名前を付けます。