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); };