From 0142134d336d7572b0fc6af2034d90cf4953e932 Mon Sep 17 00:00:00 2001 From: baz Date: Tue, 4 Jun 2024 20:56:10 +0100 Subject: [PATCH] Add Follow Player setting to CameraVolume --- Source/tank/CameraVolume.cpp | 30 ++++++++++++++++++++++++++---- Source/tank/CameraVolume.h | 20 ++++++++++++++++++++ 2 files changed, 46 insertions(+), 4 deletions(-) diff --git a/Source/tank/CameraVolume.cpp b/Source/tank/CameraVolume.cpp index 015f81b..f316260 100644 --- a/Source/tank/CameraVolume.cpp +++ b/Source/tank/CameraVolume.cpp @@ -3,9 +3,12 @@ #include "CameraVolume.h" #include "TankPlayerCharacter.h" +#include "Kismet/KismetMathLibrary.h" ACameraVolume::ACameraVolume() { + PrimaryActorTick.bCanEverTick = true; + ArrowComponent = CreateDefaultSubobject(TEXT("Arrow Component")); ArrowComponent->SetupAttachment(RootComponent); @@ -18,15 +21,34 @@ void ACameraVolume::BeginPlay() { Super::BeginPlay(); this->OnActorBeginOverlap.AddDynamic(this, &ACameraVolume::OnBeginOverlap); + this->OnActorEndOverlap.AddDynamic(this, &ACameraVolume::ACameraVolume::OnEndOverlap); +} + +void ACameraVolume::Tick(float DeltaSeconds) +{ + Super::Tick(DeltaSeconds); + + if (IsActive && FollowPlayer && TankPlayerCharacter) + { + TankPlayerCharacter->CameraComponent->SetWorldRotation(UKismetMathLibrary::FindLookAtRotation( + ArrowComponent->GetComponentTransform().GetLocation(), TankPlayerCharacter->GetTransform().GetLocation())); + } } void ACameraVolume::OnBeginOverlap(AActor* OverlappedActor, AActor* OtherActor) { if (ATankPlayerCharacter* PlayerCharacter = Cast(OtherActor)) { - FTransform transform = PlayerCharacter->CameraComponent->GetComponentTransform(); - transform.SetLocation(ArrowComponent->GetComponentTransform().GetLocation()); - transform.SetRotation(ArrowComponent->GetComponentTransform().GetRotation()); - PlayerCharacter->CameraComponent->SetWorldTransform(transform); + PlayerCharacter->CameraComponent->SetWorldLocation(ArrowComponent->GetComponentTransform().GetLocation()); + PlayerCharacter->CameraComponent->SetWorldRotation(ArrowComponent->GetComponentTransform().GetRotation()); + + TankPlayerCharacter = PlayerCharacter; + IsActive = true; } } + +void ACameraVolume::OnEndOverlap(AActor* OverlappedActor, AActor* OtherActor) +{ + IsActive = false; + TankPlayerCharacter = nullptr; +} diff --git a/Source/tank/CameraVolume.h b/Source/tank/CameraVolume.h index 32dc4a6..7c9876e 100644 --- a/Source/tank/CameraVolume.h +++ b/Source/tank/CameraVolume.h @@ -3,6 +3,7 @@ #pragma once #include "CoreMinimal.h" +#include "TankPlayerCharacter.h" #include "Camera/CameraComponent.h" #include "Components/ArrowComponent.h" #include "Engine/TriggerVolume.h" @@ -23,12 +24,31 @@ public: UPROPERTY(EditAnywhere, BlueprintReadWrite) UCameraComponent* DebugCamera; + UPROPERTY(EditAnywhere, BlueprintReadWrite) + bool FollowPlayer = false; + +private: + + UPROPERTY() + bool IsActive = false; + + UPROPERTY() + ATankPlayerCharacter* TankPlayerCharacter = nullptr; + +public: + ACameraVolume(); protected: virtual void BeginPlay() override; +public: + virtual void Tick(float DeltaSeconds) override; + private: UFUNCTION() void OnBeginOverlap(AActor* OverlappedActor, AActor* OtherActor); + + UFUNCTION() + void OnEndOverlap(AActor* OverlappedActor, AActor* OtherActor); };