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

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

UE5 C++

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

๊ฐœ์š”

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

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

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


๋ณธ๋ก 

Bullet Class

Bullet.h

์ด์•Œ์„ ๊ตฌ์„ฑํ•  ์ปดํฌ๋„ŒํŠธ 3๊ฐœ๋ฅผ ์„ ์–ธํ•ด์ฃผ๊ฒ ์Šต๋‹ˆ๋‹ค.

ํƒ„์„ฑ์„ ๊ฐ€์ง€๋Š” ์ด์•Œ์€ ์–ธ๋ฆฌ์–ผ์—์„œ ์ œ๊ณตํ•˜๋Š” ๋ฐœ์‚ฌ์ฒด ํ˜•ํƒœ์˜ ๋ฌผ๋ฆฌ ๊ธฐ๋Šฅ์„ ๊ฐ€์ง„ ์ปดํฌ๋„ŒํŠธ๋ฅผ ์‚ฌ์šฉํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค.

UProjectileMovementComponent ์œ ํ˜•์˜ ๋ฉค๋ฒ„ ๋ณ€์ˆ˜๋ฅผ ์ถ”๊ฐ€ํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค.

	UPROPERTY(VisibleAnywhere, Category=Movement)
	class UProjectileMovementComponent* movementComp;

๊ทธ๋ฆฌ๊ณ  ์ด์•Œ์˜ ์™ธ๊ด€์„ ๋‹ด๋‹นํ•  Static Mesh์™€
์ถฉ๋Œ์„ ๊ฐ์ง€ํ•  ์ปดํฌ๋„ŒํŠธ ๋˜ํ•œ ๋งŒ๋“ค์–ด ์ฃผ๊ฒ ์Šต๋‹ˆ๋‹ค.

	UPROPERTY(VisibleAnywhere,Category=Collision)
	class USphereComponent* collisionComp;
    
    	UPROPERTY(VisibleAnywhere,Category=BodyMesh)
	class UStaticMeshComponent* bodyMeshComp;

Bullet.cpp

collisionComp ์ดˆ๊ธฐํ™”

์ถฉ๋Œ์ฒด ์ธ์Šคํ„ด์Šค๋ฅผ ๋จผ์ € ๋“ฑ๋กํ•˜๊ณ  ๋ฃจํŠธ ์ปดํฌ๋„ŒํŠธ๋กœ ๋“ฑ๋กํ•ฉ๋‹ˆ๋‹ค.

	collisionComp = CreateDefaultSubobject<USphereComponent>(TEXT("CollisionComp"));
	collisionComp->SetCollisionProfileName(TEXT("BlockAll"));
	collisionComp->SetSphereRadius(13);
	RootComponent = collisionComp;

bodyMeshComp ์ดˆ๊ธฐํ™”

bodyMeshComp์—๊ฒŒ Sphere์˜ Static Mesh๋ฅผ ํ• ๋‹นํ•˜๊ณ  ์œ„์น˜๋ฅผ ์กฐ์ •ํ•ด์ฃผ์—ˆ์Šต๋‹ˆ๋‹ค.

	bodyMeshComp = CreateDefaultSubobject<UStaticMeshComponent>(TEXT("BodyMeshComp"));
	bodyMeshComp->SetupAttachment(collisionComp);
	bodyMeshComp->SetCollisionEnabled(ECollisionEnabled::NoCollision);
	bodyMeshComp->SetRelativeScale3D(FVector(0.25f));
	bodyMeshComp->SetRelativeLocation(FVector(0, 0, -12.5f));

	ConstructorHelpers::FObjectFinder<UStaticMesh>sphereMesh(TEXT("/Script/Engine.StaticMesh'/Game/StarterContent/Shapes/Shape_Sphere.Shape_Sphere'"));

	if (sphereMesh.Succeeded())
	{
		bodyMeshComp->SetStaticMesh(sphereMesh.Object);
	}

ํ—ค๋” ์ถ”๊ฐ€

ProjectileMovementComponent๋ฅผ ์‚ฌ์šฉํ•˜๊ธฐ ์œ„ํ•ด ํ—ค๋”๋ฅผ ์ถ”๊ฐ€ํ•ด์ฃผ๊ฒ ์Šต๋‹ˆ๋‹ค.

#include <GameFramework/ProjectileMovementComponent.h>

movement

์ด์•Œ์˜ ์†๋„์™€ ํƒ„์„ฑ์— ๋Œ€ํ•œ ์˜ต์…˜์„ ์„ค์ •ํ•ด์ฃผ์—ˆ์Šต๋‹ˆ๋‹ค.

movementComp = CreateDefaultSubobject<UProjectileMovementComponent>(TEXT("MovementComp"));
movementComp->SetUpdatedComponent(collisionComp);
movementComp->InitialSpeed = 5000;
movementComp->MaxSpeed = 5000;
movementComp->bShouldBounce = true;
movementComp->Bounciness = 0.3f;

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

๊ทธ๋ฆฌ๊ณ  Bulletํด๋ž˜์Šค๋ฅผ ๊ธฐ๋ฐ˜์œผ๋กœ ํ•œ BP_Bullet๋ฅผ ์ƒ์„ฑํ•ด์ฃผ์„ธ์š”.

์†Œ์ผ“์— ์ด ์žฅ์ฐฉํ•˜๊ธฐ

์†Œ์ผ“ ์ƒ์„ฑ

์šฐ์„  ์šฐ๋ฆฌ๊ฐ€ ์‚ฌ์šฉํ•˜๋Š” ์บ๋ฆญํ„ฐ์˜ Skeletal์— ๋“ค์–ด๊ฐ€์„œ ์˜ค๋ฅธ์†์— RifleSocket์„ ์ถ”๊ฐ€ํ•ด์ฃผ๊ฒ ์Šต๋‹ˆ๋‹ค.

์œ„์น˜๋Š” ๋Œ€์ถฉ ์กฐ์ •ํ•ด์ฃผ์—ˆ์Šต๋‹ˆ๋‹ค.

์ด ์žฅ์ฐฉํ•˜๊ธฐ

gunMeshComp ์„ ์–ธ

์ œ๊ฐ€ ๊ฐ€์ง€๊ณ  ์žˆ๋Š” rifle์ด skeletal mesh๋ผ์„œ USkeletalMeshComponent๋ฅผ ์‚ฌ์šฉํ•˜์˜€์Šต๋‹ˆ๋‹ค.

๋งŒ์•ฝ, ๋ณธ์ธ์˜ ์ด ์—์…‹์ด StaticMesh๋ผ๋ฉด UStaticMeshComponent๋กœ ํ•˜๋ฉด ๋˜๊ฒ ์ฃ ?

	UPROPERTY(VisibleAnywhere,Category=GunMesh)
	class USkeletalMeshComponent* gunMeshComp;

์ด ์—์…‹ ๋ถˆ๋Ÿฌ์˜ค๊ธฐ

ConstructorHelpers๋ฅผ ์‚ฌ์šฉํ•ด์„œ ๊ฐ€์ง€๊ณ  ์žˆ๋Š” ์ด ์—์…‹์„ ๋ถˆ๋Ÿฌ ์˜ค๊ฒ ์Šต๋‹ˆ๋‹ค.

ConstructorHelpers::FObjectFinder<USkeletalMesh>rifleMesh(TEXT("/Script/Engine.SkeletalMesh'/Game/MilitaryWeapSilver/Weapons/Assault_Rifle_A.Assault_Rifle_A'"));

์ดˆ๊ธฐํ™”

์šฐ์„  gunMeshComp๋ฅผ ์ธ์Šคํ„ด์Šคํ™” ํ•ด์ฃผ๊ณ 

	gunMeshComp = CreateDefaultSubobject<USkeletalMeshComponent>(TEXT("GunMeshComp"));
	gunMeshComp->SetupAttachment(GetMesh());

rifleMesh๊ฐ€ ์ •์ƒ์ ์œผ๋กœ ํ˜ธ์ถœ๋˜์—ˆ๋‹ค๋ฉด ๋ฉ”์‹œ์— ํ• ๋‹นํ•ด์ฃผ์—ˆ์Šต๋‹ˆ๋‹ค.

	if (rifleMesh.Succeeded())
	{
		gunMeshComp->SetSkeletalMesh(rifleMesh.Object);
	}

์†Œ์ผ“ ์žฅ์ฐฉ

์†Œ์ผ“์— ์žฅ์ฐฉํ•˜๋Š” ๋ฐฉ๋ฒ•์€ ๋งค์šฐ ๊ฐ„๋‹จํ•ฉ๋‹ˆ๋‹ค.
SetupAttachment()์˜ 2๋ฒˆ์งธ ์ธ์ž๋กœ ์†Œ์ผ“์˜ ์ด๋ฆ„์„ ๋„˜๊ฒจ์ฃผ๋ฉด ๋ฉ๋‹ˆ๋‹ค.

	if (rifleMesh.Succeeded())
	{
		gunMeshComp->SetSkeletalMesh(rifleMesh.Object);
        // ์ถ”๊ฐ€
        gunMeshComp->SetupAttachment(GetMesh(), TEXT("RifleSocket"));
	}

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

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


๋งˆ๋ฌด๋ฆฌ

๋‹ค์Œ ํฌ์ŠคํŒ…์—๋Š” ์ขŒํด๋ฆญ์„ Fire()์™€ ๋ฐ”์ธ๋”ฉํ•˜์—ฌ ์ด์•Œ์„ ๋ฐœ์‚ฌํ•ด๋ณด๋„๋ก ํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค.

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

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

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