발사체 생성

This commit is contained in:
강민제 2025-04-28 23:54:06 +09:00
parent b1fcccad48
commit 9484302667
7 changed files with 35 additions and 15 deletions

Binary file not shown.

View File

@ -3,6 +3,7 @@
#include "BasePawn.h" #include "BasePawn.h"
#include "Projectile.h"
#include "Components/CapsuleComponent.h" #include "Components/CapsuleComponent.h"
#include "Kismet/GameplayStatics.h" #include "Kismet/GameplayStatics.h"
@ -25,7 +26,6 @@ ABasePawn::ABasePawn()
ProjectileSpawnPoint->SetupAttachment(TurretMesh); ProjectileSpawnPoint->SetupAttachment(TurretMesh);
} }
// Rotate Turret to Target Location
void ABasePawn::RotateTurret(FVector LookAtTarget) void ABasePawn::RotateTurret(FVector LookAtTarget)
{ {
FVector ToTarget = LookAtTarget - TurretMesh->GetComponentLocation(); FVector ToTarget = LookAtTarget - TurretMesh->GetComponentLocation();
@ -40,7 +40,6 @@ void ABasePawn::RotateTurret(FVector LookAtTarget)
); );
} }
// Fire Projectile at Projectile Spawn Point
void ABasePawn::Fire() void ABasePawn::Fire()
{ {
DrawDebugSphere( DrawDebugSphere(
@ -52,4 +51,7 @@ void ABasePawn::Fire()
false, false,
3.0 3.0
); );
FVector Location = ProjectileSpawnPoint->GetComponentLocation();
FRotator Rotation = ProjectileSpawnPoint->GetComponentRotation();
GetWorld()->SpawnActor<AProjectile>(ProjectileClass, Location, Rotation);
} }

View File

@ -16,8 +16,10 @@ public:
ABasePawn(); ABasePawn();
protected: protected:
// Rotate Turret to Target Location
void RotateTurret(FVector LookAtTarget); void RotateTurret(FVector LookAtTarget);
// Fire Projectile at Projectile Spawn Point
void Fire(); void Fire();
private: private:
@ -36,5 +38,8 @@ private:
// Scene Component of Projectile Spawn Point // Scene Component of Projectile Spawn Point
UPROPERTY(VisibleAnywhere, BlueprintReadOnly, Category = "Component", meta=(AllowPrivateAccess = "true")) UPROPERTY(VisibleAnywhere, BlueprintReadOnly, Category = "Component", meta=(AllowPrivateAccess = "true"))
USceneComponent* ProjectileSpawnPoint; USceneComponent* ProjectileSpawnPoint;
UPROPERTY(EditDefaultsOnly, Category = "Combat")
TSubclassOf<class AProjectile> ProjectileClass;
}; };

View File

@ -55,7 +55,6 @@ void ATank::BeginPlay()
PlayerControllerRef = Cast<APlayerController>(GetController()); PlayerControllerRef = Cast<APlayerController>(GetController());
} }
// Move operation through W/S key
void ATank::Move(float Value) void ATank::Move(float Value)
{ {
FVector DeltaLocation = FVector::ZeroVector; FVector DeltaLocation = FVector::ZeroVector;
@ -64,7 +63,6 @@ void ATank::Move(float Value)
this->AddActorLocalOffset(DeltaLocation, true); this->AddActorLocalOffset(DeltaLocation, true);
} }
// Turn operation through A/D key
void ATank::Turn(float Value) void ATank::Turn(float Value)
{ {
FRotator DeltaRotation = FRotator::ZeroRotator; FRotator DeltaRotation = FRotator::ZeroRotator;

View File

@ -28,21 +28,29 @@ protected:
virtual void BeginPlay() override; virtual void BeginPlay() override;
private: private:
// USpringArmComponent for Camera
UPROPERTY(VisibleAnywhere, BlueprintReadOnly, Category = "Component", meta=(AllowPrivateAccess = "true")) UPROPERTY(VisibleAnywhere, BlueprintReadOnly, Category = "Component", meta=(AllowPrivateAccess = "true"))
class USpringArmComponent* SpringArm; class USpringArmComponent* SpringArm;
// UCameraComponent for Camera Component
UPROPERTY(VisibleAnywhere, BlueprintReadOnly, Category = "Component", meta=(AllowPrivateAccess = "true")) UPROPERTY(VisibleAnywhere, BlueprintReadOnly, Category = "Component", meta=(AllowPrivateAccess = "true"))
class UCameraComponent* Camera; class UCameraComponent* Camera;
// Movement speed for Move() operation
UPROPERTY(EditAnywhere, Category = "Movement", BlueprintReadWrite, meta=(AllowPrivateAccess="true")) UPROPERTY(EditAnywhere, Category = "Movement", BlueprintReadWrite, meta=(AllowPrivateAccess="true"))
float Speed = 400.0f; float Speed = 400.0f;
// Turn speed for Turn() operation
UPROPERTY(EditAnywhere, Category = "Movement") UPROPERTY(EditAnywhere, Category = "Movement")
float TurnRate = 45.0f; float TurnRate = 45.0f;
// PlayerController Pointer for Mouse Location Detection
UPROPERTY()
APlayerController* PlayerControllerRef; APlayerController* PlayerControllerRef;
// Move operation through W/S key
void Move(float Value); void Move(float Value);
// Turn operation through A/D key
void Turn(float Value); void Turn(float Value);
}; };

View File

@ -18,10 +18,10 @@ void ATower::BeginPlay()
{ {
Super::BeginPlay(); Super::BeginPlay();
Tank = Cast<ATank>(UGameplayStatics::GetPlayerPawn(this, 0)); Tank = Cast<ATank>(UGameplayStatics::GetPlayerPawn(this, 0));
GetWorldTimerManager().SetTimer(FireRateTimerHandle, this, &ATower::CheckFireCondition, FireRate, true); GetWorldTimerManager().SetTimer(FireRateTimerHandle, this, &ATower::TryFire, FireRate, true);
} }
void ATower::CheckFireCondition() void ATower::TryFire()
{ {
if (InFireRange()) if (InFireRange())
{ {
@ -29,7 +29,7 @@ void ATower::CheckFireCondition()
} }
} }
bool ATower::InFireRange() bool ATower::InFireRange() const
{ {
if (Tank) if (Tank)
{ {

View File

@ -21,16 +21,23 @@ protected:
virtual void BeginPlay() override; virtual void BeginPlay() override;
private: private:
// Tank Object Pointer which is target
class ATank* Tank; class ATank* Tank;
// Distance to Start Tracking the target
UPROPERTY(EditAnywhere, Category = "Rotate") UPROPERTY(EditAnywhere, Category = "Rotate")
float FireRange = 400; float FireRange = 400.0f;
// FTimerHandle for SetTimer.
FTimerHandle FireRateTimerHandle; FTimerHandle FireRateTimerHandle;
float FireRate = 2.0f;
void CheckFireCondition();
bool InFireRange(); // Fire Period
float FireRate = 2.0f;
// Check Tank is in Range. If so, Fire operation occurs.
void TryFire();
// Check Tank is in Fire Range,
bool InFireRange() const;
}; };