From 5be0c02e6929a8871549c496915cd4d89a982978 Mon Sep 17 00:00:00 2001 From: baz Date: Tue, 4 Jun 2024 20:24:02 +0100 Subject: [PATCH] Add Camera Volume --- Source/tank/CameraVolume.cpp | 32 +++++++++++++++++++++++++++ Source/tank/CameraVolume.h | 34 +++++++++++++++++++++++++++++ Source/tank/TankPlayerCharacter.cpp | 1 + Source/tank/TankPlayerCharacter.h | 4 ++++ 4 files changed, 71 insertions(+) create mode 100644 Source/tank/CameraVolume.cpp create mode 100644 Source/tank/CameraVolume.h diff --git a/Source/tank/CameraVolume.cpp b/Source/tank/CameraVolume.cpp new file mode 100644 index 0000000..015f81b --- /dev/null +++ b/Source/tank/CameraVolume.cpp @@ -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(TEXT("Arrow Component")); + ArrowComponent->SetupAttachment(RootComponent); + + DebugCamera = CreateDefaultSubobject(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(OtherActor)) + { + FTransform transform = PlayerCharacter->CameraComponent->GetComponentTransform(); + transform.SetLocation(ArrowComponent->GetComponentTransform().GetLocation()); + transform.SetRotation(ArrowComponent->GetComponentTransform().GetRotation()); + PlayerCharacter->CameraComponent->SetWorldTransform(transform); + } +} diff --git a/Source/tank/CameraVolume.h b/Source/tank/CameraVolume.h new file mode 100644 index 0000000..32dc4a6 --- /dev/null +++ b/Source/tank/CameraVolume.h @@ -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); +}; diff --git a/Source/tank/TankPlayerCharacter.cpp b/Source/tank/TankPlayerCharacter.cpp index e78a031..b791590 100644 --- a/Source/tank/TankPlayerCharacter.cpp +++ b/Source/tank/TankPlayerCharacter.cpp @@ -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(TEXT("Camera Component")); } // Called when the game starts or when spawned diff --git a/Source/tank/TankPlayerCharacter.h b/Source/tank/TankPlayerCharacter.h index 63f75ad..e46601f 100644 --- a/Source/tank/TankPlayerCharacter.h +++ b/Source/tank/TankPlayerCharacter.h @@ -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();