Add SplineCameraVolume

This commit is contained in:
baz 2024-06-06 00:12:02 +01:00
parent f1a71a0a0e
commit 66a782609d
4 changed files with 64 additions and 8 deletions

View File

@ -10,7 +10,7 @@
ACameraVolume::ACameraVolume() ACameraVolume::ACameraVolume()
{ {
PrimaryActorTick.bCanEverTick = true; PrimaryActorTick.bCanEverTick = true;
ArrowComponent = CreateDefaultSubobject<UArrowComponent>(TEXT("Arrow Component")); ArrowComponent = CreateDefaultSubobject<UArrowComponent>(TEXT("Arrow Component"));
ArrowComponent->SetupAttachment(RootComponent); ArrowComponent->SetupAttachment(RootComponent);
@ -23,7 +23,7 @@ void ACameraVolume::BeginPlay()
{ {
Super::BeginPlay(); Super::BeginPlay();
this->OnActorBeginOverlap.AddDynamic(this, &ACameraVolume::OnBeginOverlap); this->OnActorBeginOverlap.AddDynamic(this, &ACameraVolume::OnBeginOverlap);
this->OnActorEndOverlap.AddDynamic(this, &ACameraVolume::ACameraVolume::OnEndOverlap); this->OnActorEndOverlap.AddDynamic(this, &ACameraVolume::OnEndOverlap);
} }
void ACameraVolume::Tick(float DeltaSeconds) void ACameraVolume::Tick(float DeltaSeconds)
@ -62,7 +62,7 @@ void ACameraVolume::OnBeginOverlap(AActor* OverlappedActor, AActor* OtherActor)
{ {
TankPlayerCharacter = PlayerCharacter; TankPlayerCharacter = PlayerCharacter;
IsActive = true; IsActive = true;
auto CameraManagerSubsystem = GetGameInstance()->GetSubsystem<UCameraManagerSubsystem>(); auto CameraManagerSubsystem = GetGameInstance()->GetSubsystem<UCameraManagerSubsystem>();
CameraManagerSubsystem->SetCurrentCameraVolume(this); CameraManagerSubsystem->SetCurrentCameraVolume(this);
} }
@ -72,7 +72,7 @@ void ACameraVolume::OnEndOverlap(AActor* OverlappedActor, AActor* OtherActor)
{ {
IsActive = false; IsActive = false;
TankPlayerCharacter = nullptr; TankPlayerCharacter = nullptr;
auto CameraManagerSubsystem = GetGameInstance()->GetSubsystem<UCameraManagerSubsystem>(); auto CameraManagerSubsystem = GetGameInstance()->GetSubsystem<UCameraManagerSubsystem>();
CameraManagerSubsystem->RemoveCameraVolume(this); CameraManagerSubsystem->RemoveCameraVolume(this);
} }

View File

@ -26,17 +26,15 @@ public:
UPROPERTY(EditAnywhere, BlueprintReadWrite) UPROPERTY(EditAnywhere, BlueprintReadWrite)
bool FollowPlayer = false; bool FollowPlayer = false;
private:
protected:
UPROPERTY() UPROPERTY()
bool IsActive = false; bool IsActive = false;
UPROPERTY() UPROPERTY()
ATankPlayerCharacter* TankPlayerCharacter = nullptr; ATankPlayerCharacter* TankPlayerCharacter = nullptr;
public: public:
ACameraVolume(); ACameraVolume();
protected: protected:

View File

@ -0,0 +1,31 @@
// Fill out your copyright notice in the Description page of Project Settings.
#include "SplineCameraVolume.h"
#include "Kismet/KismetMathLibrary.h"
ASplineCameraVolume::ASplineCameraVolume()
{
SplineComponent = CreateDefaultSubobject<USplineComponent>(TEXT("Spline Component"));
SplineComponent->SetupAttachment(RootComponent);
FollowPlayer = true;
}
void ASplineCameraVolume::BeginPlay()
{
Super::BeginPlay();
}
void ASplineCameraVolume::Tick(float DeltaSeconds)
{
if (IsActive && FollowPlayer && TankPlayerCharacter)
{
FVector Location = SplineComponent->FindLocationClosestToWorldLocation(
TankPlayerCharacter->GetTransform().GetLocation(), ESplineCoordinateSpace::World);
TankPlayerCharacter->CameraComponent->SetWorldLocation(Location);
TankPlayerCharacter->CameraComponent->SetWorldRotation(UKismetMathLibrary::FindLookAtRotation(
Location, TankPlayerCharacter->GetTransform().GetLocation()));
}
}

View File

@ -0,0 +1,27 @@
// Fill out your copyright notice in the Description page of Project Settings.
#pragma once
#include "CoreMinimal.h"
#include "CameraVolume.h"
#include "Components/SplineComponent.h"
#include "SplineCameraVolume.generated.h"
/**
*
*/
UCLASS()
class TANK_API ASplineCameraVolume : public ACameraVolume
{
GENERATED_BODY()
public:
UPROPERTY(EditAnywhere, BlueprintReadWrite)
USplineComponent* SplineComponent;
ASplineCameraVolume();
virtual void BeginPlay() override;
virtual void Tick(float DeltaSeconds) override;
};