From 66a782609d8e01cd64a941a4ddae9bae0f1c68c1 Mon Sep 17 00:00:00 2001 From: baz Date: Thu, 6 Jun 2024 00:12:02 +0100 Subject: [PATCH] Add SplineCameraVolume --- Source/tank/CameraVolume.cpp | 8 ++++---- Source/tank/CameraVolume.h | 6 ++---- Source/tank/SplineCameraVolume.cpp | 31 ++++++++++++++++++++++++++++++ Source/tank/SplineCameraVolume.h | 27 ++++++++++++++++++++++++++ 4 files changed, 64 insertions(+), 8 deletions(-) create mode 100644 Source/tank/SplineCameraVolume.cpp create mode 100644 Source/tank/SplineCameraVolume.h diff --git a/Source/tank/CameraVolume.cpp b/Source/tank/CameraVolume.cpp index 4aa5f29..5d1a36e 100644 --- a/Source/tank/CameraVolume.cpp +++ b/Source/tank/CameraVolume.cpp @@ -10,7 +10,7 @@ ACameraVolume::ACameraVolume() { PrimaryActorTick.bCanEverTick = true; - + ArrowComponent = CreateDefaultSubobject(TEXT("Arrow Component")); ArrowComponent->SetupAttachment(RootComponent); @@ -23,7 +23,7 @@ void ACameraVolume::BeginPlay() { Super::BeginPlay(); this->OnActorBeginOverlap.AddDynamic(this, &ACameraVolume::OnBeginOverlap); - this->OnActorEndOverlap.AddDynamic(this, &ACameraVolume::ACameraVolume::OnEndOverlap); + this->OnActorEndOverlap.AddDynamic(this, &ACameraVolume::OnEndOverlap); } void ACameraVolume::Tick(float DeltaSeconds) @@ -62,7 +62,7 @@ void ACameraVolume::OnBeginOverlap(AActor* OverlappedActor, AActor* OtherActor) { TankPlayerCharacter = PlayerCharacter; IsActive = true; - + auto CameraManagerSubsystem = GetGameInstance()->GetSubsystem(); CameraManagerSubsystem->SetCurrentCameraVolume(this); } @@ -72,7 +72,7 @@ void ACameraVolume::OnEndOverlap(AActor* OverlappedActor, AActor* OtherActor) { IsActive = false; TankPlayerCharacter = nullptr; - + auto CameraManagerSubsystem = GetGameInstance()->GetSubsystem(); CameraManagerSubsystem->RemoveCameraVolume(this); } diff --git a/Source/tank/CameraVolume.h b/Source/tank/CameraVolume.h index 516793f..c9f25c6 100644 --- a/Source/tank/CameraVolume.h +++ b/Source/tank/CameraVolume.h @@ -26,17 +26,15 @@ public: UPROPERTY(EditAnywhere, BlueprintReadWrite) bool FollowPlayer = false; - -private: +protected: UPROPERTY() bool IsActive = false; UPROPERTY() ATankPlayerCharacter* TankPlayerCharacter = nullptr; - + public: - ACameraVolume(); protected: diff --git a/Source/tank/SplineCameraVolume.cpp b/Source/tank/SplineCameraVolume.cpp new file mode 100644 index 0000000..9447e14 --- /dev/null +++ b/Source/tank/SplineCameraVolume.cpp @@ -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(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())); + } +} diff --git a/Source/tank/SplineCameraVolume.h b/Source/tank/SplineCameraVolume.h new file mode 100644 index 0000000..b37e873 --- /dev/null +++ b/Source/tank/SplineCameraVolume.h @@ -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; +};