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 "TankPlayerCharacter.h"
#include "Kismet/KismetMathLibrary.h"
ACameraVolume::ACameraVolume()
{
PrimaryActorTick.bCanEverTick = true;
ArrowComponent = CreateDefaultSubobject<UArrowComponent>(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<ATankPlayerCharacter>(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;
}

View File

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