📄 총알 발사하기
✏️ TPSPlayer.h
void OnInputFire();
UPROPERTY(EditAnywhere)
TSubclassOf<class ABulletActor> bulletFactory;
UPROPERTY(EditAnywhere)
class USkeletalMeshComponent* gunMeshComp;
✏️ TPSPlayer.cpp
ATPSPlayer::ATPSPlayer()
{
gunMeshComp = CreateDefaultSubobject<USkeletalMeshComponent>(TEXT("gunMeshComp"));
gunMeshComp->SetupAttachment(GetMesh());
ConstructorHelpers::FObjectFinder<USkeletalMesh> tempGunMesh(TEXT("SkeletalMesh'/Game/Resources/FPWeapon/Mesh/SK_FPGun.SK_FPGun'"));
if (tempGunMesh.Succeeded())
{
gunMeshComp->SetSkeletalMesh(tempGunMesh.Object);
gunMeshComp->SetRelativeLocation(FVector(0, 40, 140));
}
}
void ATPSPlayer::SetupPlayerInputComponent(UInputComponent* PlayerInputComponent)
{
Super::SetupPlayerInputComponent(PlayerInputComponent);
PlayerInputComponent->BindAction(TEXT("Fire"), IE_Pressed, this, &ATPSPlayer::OnInputFire);
}
void ATPSPlayer::OnInputFire()
{
FTransform firePosition = gunMeshComp->GetSocketTransform(TEXT("Muzzle"));
GetWorld()->SpawnActor<ABullet>(bulletFactory, firePosition);
}
📄 연사 (Auto Fire)
✏️ TPSPlayer.h
void OnInputFirePressed();
void OnInputFireReleased();
void OnMakeBullet();
FTimerHandle autoFireTimerHandle;
✏️ TPSPlayer.cpp
void ATPSPlayer::SetupPlayerInputComponent(UInputComponent* PlayerInputComponent)
{
Super::SetupPlayerInputComponent(PlayerInputComponent);
PlayerInputComponent->BindAction(TEXT("Fire"), IE_Pressed, this, &ATPSPlayer::OnInputFirePressed);
PlayerInputComponent->BindAction(TEXT("Fire"), IE_Released, this, &ATPSPlayer::OnInputFireReleased);
}
void ATPSPlayer::OnInputFirePressed()
{
OnMakeBullet();
GetWorldTimerManager().SetTimer(autoFireTimerHandle, this, &ATPSPlayer::OnMakeBullet, 0.15f, true, 0.15f);
}
void ATPSPlayer::OnInputFireReleased()
{
GetWorldTimerManager().ClearTimer(autoFireTimerHandle);
}
void ATPSPlayer::OnMakeBullet()
{
FTransform firePosition = gunMeshComp->GetSocketTransform(TEXT("Muzzle"));
GetWorld()->SpawnActor<ABullet>(bulletFactory, firePosition);
}