Add Camera Volume

This commit is contained in:
baz 2024-06-04 20:24:02 +01:00
parent 3678ce7e1d
commit 5be0c02e69
4 changed files with 71 additions and 0 deletions

View File

@ -0,0 +1,32 @@
// Fill out your copyright notice in the Description page of Project Settings.
#include "CameraVolume.h"
#include "TankPlayerCharacter.h"
ACameraVolume::ACameraVolume()
{
ArrowComponent = CreateDefaultSubobject<UArrowComponent>(TEXT("Arrow Component"));
ArrowComponent->SetupAttachment(RootComponent);
DebugCamera = CreateDefaultSubobject<UCameraComponent>(TEXT("Debug Camera"));
DebugCamera->SetupAttachment(ArrowComponent);
DebugCamera->SetActive(false);
}
void ACameraVolume::BeginPlay()
{
Super::BeginPlay();
this->OnActorBeginOverlap.AddDynamic(this, &ACameraVolume::OnBeginOverlap);
}
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);
}
}

View File

@ -0,0 +1,34 @@
// Fill out your copyright notice in the Description page of Project Settings.
#pragma once
#include "CoreMinimal.h"
#include "Camera/CameraComponent.h"
#include "Components/ArrowComponent.h"
#include "Engine/TriggerVolume.h"
#include "CameraVolume.generated.h"
/**
*
*/
UCLASS(Blueprintable)
class TANK_API ACameraVolume : public ATriggerVolume
{
GENERATED_BODY()
public:
UPROPERTY(EditAnywhere, BlueprintReadWrite)
UArrowComponent* ArrowComponent;
UPROPERTY(EditAnywhere, BlueprintReadWrite)
UCameraComponent* DebugCamera;
ACameraVolume();
protected:
virtual void BeginPlay() override;
private:
UFUNCTION()
void OnBeginOverlap(AActor* OverlappedActor, AActor* OtherActor);
};

View File

@ -14,6 +14,7 @@ ATankPlayerCharacter::ATankPlayerCharacter()
// Set this character to call Tick() every frame. You can turn this off to improve performance if you don't need it.
PrimaryActorTick.bCanEverTick = true;
CameraComponent = CreateDefaultSubobject<UCameraComponent>(TEXT("Camera Component"));
}
// Called when the game starts or when spawned

View File

@ -3,6 +3,7 @@
#pragma once
#include "CoreMinimal.h"
#include "Camera/CameraComponent.h"
#include "GameFramework/Character.h"
#include "TankPlayerCharacter.generated.h"
@ -26,6 +27,9 @@ public:
UPROPERTY(EditDefaultsOnly, BlueprintReadWrite)
UInputAction* YawAction;
UPROPERTY(EditAnywhere, BlueprintReadWrite)
UCameraComponent* CameraComponent;
public:
// Sets default values for this character's properties
ATankPlayerCharacter();