[UE5 / C++] ๐Ÿ”ซ ์ด์•Œ ๋ฐœ์‚ฌํ•˜๊ธฐ (ๅฎŒ)

Singery00ยท2024๋…„ 4์›” 22์ผ
0

UE5 C++

๋ชฉ๋ก ๋ณด๊ธฐ
10/20
post-thumbnail

๊ฐœ์š”

๐Ÿ’ก C++๋กœ ์ด์•Œ์„ ๋ฐœ์‚ฌํ•ด๋ณด์ž!

์ด๋ฒˆ์—๋Š” C++๋กœ ํด๋ฆญํ•˜๋ฉด ์ด์•Œ์„ ๋ฐœ์‚ฌํ•˜๋Š” ์ฝ”๋“œ๋ฅผ ์ž‘์„ฑํ•ด๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค.

1. Bullet ํด๋ž˜์Šค ์ƒ์„ฑ
2. ์†Œ์ผ“์— ์ด ์žฅ์ฐฉํ•˜๊ธฐ
3. ๋ฐœ์‚ฌ ์ž…๋ ฅ ๋ฐ›๊ธฐ
4. Spawn Actor๋กœ ์ด์•Œ ์ƒ์„ฑํ•˜๊ธฐ
5. Bullet ์†Œ๋ฉธ์ฃผ๊ธฐ


๋ณธ๋ก 

์ขŒํด๋ฆญ ์ž…๋ ฅ ๋ฐ›๊ธฐ

๋ฐ”์ธ๋”ฉ ํ•จ์ˆ˜ ์„ ์–ธ

Fire()ํ•จ์ˆ˜๋ฅผ ์„ ์–ธํ•ด์ฃผ์„ธ์š”.

void Fire();

๋งคํ•‘

์ €๋Š” Action์œผ๋กœ ๋งคํ•‘์„ ํ•ด์ฃผ์—ˆ์Šต๋‹ˆ๋‹ค.

๋ฐ”์ธ๋”ฉ

BindAction()์„ ์‚ฌ์šฉํ•ด์„œ Fire()์™€ ๋ฐ”์ธ๋”ฉํ•ด์ฃผ์„ธ์š”.

AMyPlayer::SetupPlayerInputComponent(UInputComponent* PlayerInputComponent)
{
	Super::SetupPlayerInputComponent(PlayerInputComponent);
	
    // ์ƒ๋žต

	PlayerInputComponent->BindAction(TEXT("Fire"), IE_Pressed, this, &AMyPlayer::Fire);

}

Fire() ์ •์˜

์ž„์‹œ๋กœ ๋กœ๊ทธ๋ฅผ ์ถœ๋ ฅํ•˜๋„๋ก ํ•ด์ฃผ๊ฒ ์Šต๋‹ˆ๋‹ค.

void AMyPlayer::Fire()
{
	UE_LOG(LogTemp, Warning, TEXT("Fire ON"));
}

โ—์ €์žฅ ํ›„ ์ปดํŒŒ์ผโ—ํ•ด์ฃผ์„ธ์š”.

๊ทธ๋ฆฌ๊ณ  ์‹คํ–‰ํ•ด์„œ ์ขŒํด๋ฆญ์„ ํ•ด์ฃผ๋ฉด

๋ฐ”์ธ๋”ฉ์ด ์™„๋ฃŒ๋˜์—ˆ์Šต๋‹ˆ๋‹ค.

์ด์•Œ ๋ฐœ์‚ฌํ•˜๊ธฐ

Bullet Factory ์„ ์–ธ

player.h๋กœ ๊ฐ€์…”์„œ bullet facotry๋ฅผ ์„ ์–ธํ•ด์ฃผ์„ธ์š”.

UPROPERTY(EditDefaultsOnly,Category=BulletFactory)
	TSubclassOf<class ABullet> bulletFactory;

TSubclassOf ํ…œํ”Œ๋ฆฟ ํด๋ž˜์Šค๋Š” ํƒฌํ”Œ๋ฆฟ์— ๋“ฑ๋ก๋œ ABullet์œ ํ˜•์˜ ํด๋ž˜์Šค,
ํ˜น์€ ๋ธ”๋ฃจํ”„๋ฆฐํŠธ ํด๋ž˜์Šค๋ฅผ ์–ธ๋ฆฌ์–ผ ์—๋””ํ„ฐ๋กœ๋ถ€ํ„ฐ ํ• ๋‹น๋ฐ›์„ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

Fire ์ •์˜

Bullet.h๋ฅผ ํ—ค๋”์— ์ถ”๊ฐ€ํ•ด์ฃผ์„ธ์š”.

#include "Bullet.h"

๊ทธ๋ฆฌ๊ณ  ์•„๋ž˜์™€ ๊ฐ™์ด ์ฝ”๋“œ๋ฅผ ์ž‘์„ฑํ•ด์ฃผ์„ธ์š”.

void AMyPlayer::Fire()
{
	FTransform fireposition = gunMeshComp->GetSocketTransform(TEXT("FirePosition"));
	GetWorld()->SpawnActor<ABullet>(bulletFactory, fireposition);
}

๋ฐœ์‚ฌ๋  ์œ„์น˜์˜ ํฌ์ง€์…˜์„ ์ฐพ๊ณ ,
ํ•ด๋‹น ์œ„์น˜์—์„œ ์ด์•Œ์„ ์ƒ์„ฑํ•ฉ๋‹ˆ๋‹ค.

โ—์ €์žฅ ํ›„ ์ปดํŒŒ์ผโ—ํ•ด์ฃผ์„ธ์š”.
๊ทธ๋Ÿฐ ํ›„ ํ”Œ๋ ˆ์ด ํ•ด๋ณด๋ฉด

ํ”Œ๋ ˆ์ด์–ด์˜ ์บ๋ฆญํ„ฐ ์˜ค๋ฅธ์†์— ์ด์ด ๋“ค๋ ค์žˆ๋Š” ๊ฒƒ์„ ํ™•์ธํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

์ถ”๊ฐ€์„ค์ •

FirePosition ์†Œ์ผ“ ์ถ”๊ฐ€

๊ฐ์ž๊ฐ€ ์‚ฌ์šฉํ•œ ๋ฌด๊ธฐ์˜ Skeletal Mesh๋ฅผ ์ผœ์ฃผ์„ธ์š”.
๊ทธ๋ฆฌ๊ณ  ์†Œ์ผ“์„ ์ถ”๊ฐ€ํ•œ ํ›„ ์ด๊ตฌ์•ž์— FirePosition์„ ๋ฐฐ์น˜ํ•ด์ค„๊ฒŒ์š”.

Bullet ํ• ๋‹น

BP_Player๋ฅผ ์—ด์–ด์„œ BulletFactory์— BP_Bullet์„ ํ• ๋‹นํ•ด์ฃผ์„ธ์š”.

โ—์ €์žฅ ํ›„ ์ปดํŒŒ์ผโ—ํ›„ ํ”Œ๋ ˆ์ด ํ•ด๋ณด๋ฉด ์ด๊ตฌ ์•ž์—์„œ ์ด์•Œ์ด ์ž˜ ๋‚˜๊ฐ‘๋‹ˆ๋‹ค.

Bullet ์†Œ๋ฉธ ์ฃผ๊ธฐ

์ง€๊ธˆ ๋ณด์‹œ๋ฉด ์ด์•Œ ์ธ์Šคํ„ด์Šค๋“ค์ด ์‚ฌ๋ผ์ง€์ง€ ์•Š๊ณ  ๊ณ„์† ์Œ“์ด๊ณ  ์žˆ์Šต๋‹ˆ๋‹ค.

๊ณ„์† ์ด์•Œ์ด ์ƒ์„ฑ๋˜๊ณ  ์ž”๋ฅ˜ํ•˜๊ธฐ๋ฅผ ๋ฐ˜๋ณตํ•˜๋ฉด ๋ฉ”๋ชจ๋ฆฌ๊ฐ€ ๋ถ€์กฑํ•ด์„œ ์•ฑ์ด ์ค‘๋‹จ๋˜๋Š” ์ƒํ™ฉ์ด ๋ฐœ์ƒํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

์šฐ๋ฆฌ๋Š” ์ƒ๋ช… ์ฃผ๊ธฐ๋ฅผ ์ฃผ๊ณ , ๊ทธ ์‹œ๊ฐ„์ด ์ง€๋‚˜๋ฉด ์†Œ๋ฉธํ•˜๋„๋ก ์ƒ์„ฑํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค.

์ƒ๋ช… ์‹œ๊ฐ„ ์ฃผ๊ธฐ

Bullet.cpp์˜ ์ƒ์„ฑ์ž๋กœ ๊ฐ€์ฃผ์„ธ์š”.
๊ทธ๋ฆฌ๊ณ  InitialLifeSpan๋ฅผ 2.0f ์ •๋„๋กœ ์„ค์ •ํ•ด์ฃผ๊ฒ ์Šต๋‹ˆ๋‹ค.

ABullet::ABullet()
{
	// ์ƒ๋žต
    
	InitialLifeSpan = 2.0f;
}

Die() ํ•จ์ˆ˜ ์„ ์–ธ

Die()ํ•จ์ˆ˜๋ฅผ ์„ ์–ธํ•ด์ฃผ์„ธ์š”.

void Die();

Die() ์ •์˜

Die๋Š” ๋‹จ์ˆœํ•˜๊ฒŒ ๊ฐ์ฒด๋ฅผ Destroy()ํ•ด์ฃผ๋ฉด ๋ฉ๋‹ˆ๋‹ค.

void ABullet::Die()
{
	Destroy();
}

Die() ํ˜ธ์ถœ

BeginPlayer์—์„œ ์ƒ์„ฑ๋œ ํ›„ ์„ค์ •ํ•œ ์ƒ๋ช…์ฃผ๊ธฐ ์‹œ๊ฐ„์ดํ›„ Die()๋ฅผ ํ˜ธ์ถœํ•ด์ฃผ๊ฒ ์Šต๋‹ˆ๋‹ค.

void ABullet::BeginPlay()
{
	Super::BeginPlay();
	
	FTimerHandle deathTimer;
	GetWorld()->GetTimerManager().SetTimer(deathTimer, this, &ABullet::Die, 2.0f, false);
}

โ—์ €์žฅ ํ›„ ์ปดํŒŒ์ผโ—ํ›„ ํ”Œ๋ ˆ์ด ํ•ด๋ณด๋ฉด ์ด์•Œ์ด ์‚ฌ๋ผ์ง€๋Š”๊ฑธ ํ™•์ธํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.


๋งˆ๋ฌด๋ฆฌ

์šฐ๋ฆฌ๋Š” ์ ์  ๊ทธ๋Ÿด์‹ธํ•˜๊ฒŒ ๊ธฐ๋Šฅ๋“ค์ด ํ•˜๋‚˜์”ฉ ์ถ”๊ฐ€๋˜๊ณ  ์žˆ์Šต๋‹ˆ๋‹ค!

๋‹ค์Œ์—๋Š” ์œ ํ•œ์ƒํƒœ๊ธฐ๊ณ„(FSM)์„ ๊ฐ€์ง„ ์ ์„ ๋งŒ๋“ค์–ด ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค.

๊ฐ์‚ฌํ•ฉ๋‹ˆ๋‹ค.

1. Bullet ํด๋ž˜์Šค ์ƒ์„ฑ
2. ์†Œ์ผ“์— ์ด ์žฅ์ฐฉํ•˜๊ธฐ
3. ๋ฐœ์‚ฌ ์ž…๋ ฅ ๋ฐ›๊ธฐ
4. Spawn Actor๋กœ ์ด์•Œ ์ƒ์„ฑํ•˜๊ธฐ
5. Bullet ์†Œ๋ฉธ์ฃผ๊ธฐ

profile
๊ฒŒ์ž„ ๊ฐœ๋ฐœ์ž๊ฐ€ ๋˜์–ด๋ณด์ž

0๊ฐœ์˜ ๋Œ“๊ธ€