Add Follow Player setting to CameraVolume

This commit is contained in:
baz 2024-06-04 20:56:10 +01:00
parent a5cfef827b
commit 0142134d33
2 changed files with 46 additions and 4 deletions

View File

@ -3,9 +3,12 @@
#include "CameraVolume.h" #include "CameraVolume.h"
#include "TankPlayerCharacter.h" #include "TankPlayerCharacter.h"
#include "Kismet/KismetMathLibrary.h"
ACameraVolume::ACameraVolume() ACameraVolume::ACameraVolume()
{ {
PrimaryActorTick.bCanEverTick = true;
ArrowComponent = CreateDefaultSubobject<UArrowComponent>(TEXT("Arrow Component")); ArrowComponent = CreateDefaultSubobject<UArrowComponent>(TEXT("Arrow Component"));
ArrowComponent->SetupAttachment(RootComponent); ArrowComponent->SetupAttachment(RootComponent);
@ -18,15 +21,34 @@ 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);
}
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) void ACameraVolume::OnBeginOverlap(AActor* OverlappedActor, AActor* OtherActor)
{ {
if (ATankPlayerCharacter* PlayerCharacter = Cast<ATankPlayerCharacter>(OtherActor)) if (ATankPlayerCharacter* PlayerCharacter = Cast<ATankPlayerCharacter>(OtherActor))
{ {
FTransform transform = PlayerCharacter->CameraComponent->GetComponentTransform(); PlayerCharacter->CameraComponent->SetWorldLocation(ArrowComponent->GetComponentTransform().GetLocation());
transform.SetLocation(ArrowComponent->GetComponentTransform().GetLocation()); PlayerCharacter->CameraComponent->SetWorldRotation(ArrowComponent->GetComponentTransform().GetRotation());
transform.SetRotation(ArrowComponent->GetComponentTransform().GetRotation());
PlayerCharacter->CameraComponent->SetWorldTransform(transform); TankPlayerCharacter = PlayerCharacter;
IsActive = true;
} }
} }
void ACameraVolume::OnEndOverlap(AActor* OverlappedActor, AActor* OtherActor)
{
IsActive = false;
TankPlayerCharacter = nullptr;
}

View File

@ -3,6 +3,7 @@
#pragma once #pragma once
#include "CoreMinimal.h" #include "CoreMinimal.h"
#include "TankPlayerCharacter.h"
#include "Camera/CameraComponent.h" #include "Camera/CameraComponent.h"
#include "Components/ArrowComponent.h" #include "Components/ArrowComponent.h"
#include "Engine/TriggerVolume.h" #include "Engine/TriggerVolume.h"
@ -23,12 +24,31 @@ public:
UPROPERTY(EditAnywhere, BlueprintReadWrite) UPROPERTY(EditAnywhere, BlueprintReadWrite)
UCameraComponent* DebugCamera; UCameraComponent* DebugCamera;
UPROPERTY(EditAnywhere, BlueprintReadWrite)
bool FollowPlayer = false;
private:
UPROPERTY()
bool IsActive = false;
UPROPERTY()
ATankPlayerCharacter* TankPlayerCharacter = nullptr;
public:
ACameraVolume(); ACameraVolume();
protected: protected:
virtual void BeginPlay() override; virtual void BeginPlay() override;
public:
virtual void Tick(float DeltaSeconds) override;
private: private:
UFUNCTION() UFUNCTION()
void OnBeginOverlap(AActor* OverlappedActor, AActor* OtherActor); void OnBeginOverlap(AActor* OverlappedActor, AActor* OtherActor);
UFUNCTION()
void OnEndOverlap(AActor* OverlappedActor, AActor* OtherActor);
}; };