[Unreal][C++] ์•กํ„ฐ ์ƒ์„ฑ

2024. 4. 13. 21:09ยท๐Ÿ“ Game/โœ Unreal

1. ํŠน์ • ์œ„์น˜์— ์ƒ์„ฑํ•˜๊ธฐ

์Šคํฐ ํฌ์ธํŠธ๊ฐ€ ๋  ๋ธ”๋ฃจํ”„๋ฆฐํŠธ ํด๋ž˜์Šค ์•กํ„ฐ๋ฅผ ์ƒ์„ฑํ•˜๊ณ  ๋ ˆ๋ฒจ ๋‚ด ๋ฐฐ์น˜ํ•œ๋‹ค. ์ด๋ ‡๊ฒŒ ๋ฐฐ์น˜๋œ ์Šคํฐ ํฌ์ธํŠธ ์ค‘ ๋žœ๋คํ•œ ์Šคํฐ ํฌ์ธํŠธ์˜ ์œ„์น˜์— ์•กํ„ฐ๊ฐ€ ์ƒ์„ฑ๋˜๋„๋ก ํ•  ๊ฒƒ์ด๋‹ค.

 

์Šคํฐ ํฌ์ธํŠธ ์•กํ„ฐ ์ƒ์„ฑ

 

1) ๋ธ”๋ฃจํ”„๋ฆฐํŠธ ๊ตฌํ˜„

๋จผ์ € ๋ฐฐ์น˜๋œ ์Šคํฐ ํฌ์ธํŠธ๋“ค์„ ๊ฐ€์ ธ์˜ฌ ๊ฒƒ์ด๋‹ค. Get All Actors Of Class With Tag ๋…ธ๋“œ๋ฅผ ํ†ตํ•ด ๋ ˆ๋ฒจ์— ๋ฐฐ์น˜๋œ ์•กํ„ฐ๋“ค์„ ๊ฐ€์ ธ์˜ฌ ์ˆ˜ ์žˆ๋‹ค. Tag๋ฅผ ํ†ตํ•ด ํŠน์ • Tag๋ฅผ ๊ฐ€์ง„ ์•กํ„ฐ๋งŒ ๊ฐ€์ ธ์˜ค๊ธฐ๋„ ๊ฐ€๋Šฅํ•˜๋‹ค.

 

Get All Actors Of Class With Tag ๋…ธ๋“œ

 

๋‹ค์Œ์œผ๋กœ ์ƒ์„ฑํ•˜๊ณ  ์‹ถ์€ ์ˆ˜๋งŒํผ Loop๋ฅผ ๋Œ๋ฉฐ ์•กํ„ฐ๋ฅผ ์ƒ์„ฑํ•œ๋‹ค.

  • Collision Handling Override
    • Always Spawn, Ignore Collisions: ์•กํ„ฐ๊ฐ€ ๋‹ค๋ฅธ ์˜ค๋ธŒ์ ํŠธ์™€ ์ถฉ๋Œํ•ด๋„ ์ง€์ •๋œ ์œ„์น˜์— ์ƒ์„ฑํ•œ๋‹ค
    • Try To Adjust Location, But Always Spawn: ์ถฉ๋Œ์„ ํ”ผํ•˜๊ธฐ ์œ„ํ•ด ์•กํ„ฐ์˜ ์œ„์น˜๋ฅผ ์กฐ์ •ํ•˜๋ ค๊ณ  ์‹œ๋„ํ•œ๋‹ค. ๋งŒ์•ฝ ์ถฉ๋Œ์„ ํ”ผํ•  ์ˆ˜ ์žˆ๋Š” ์œ„์น˜๋ฅผ ์ฐพ์ง€ ๋ชปํ•œ๋‹ค๋ฉด, ์ถฉ๋Œ์„ ๋ฌด์‹œํ•˜๊ณ  ์ง€์ •๋œ ์œ„์น˜์— ์•กํ„ฐ๋ฅผ ์ƒ์„ฑํ•œ๋‹ค.
    • Try To Adjust Location, Don't Spawn If Still Colliding: ์ถฉ๋Œ์„ ํ”ผํ•˜๊ธฐ ์œ„ํ•ด ์•กํ„ฐ์˜ ์œ„์น˜๋ฅผ ์กฐ์ •ํ•˜๋ ค๊ณ  ์‹œ๋„ํ•œ๋‹ค. ์—ฌ์ „ํžˆ ์ถฉ๋Œ์ด ๋ฐœ์ƒํ•œ๋‹ค๋ฉด, ์•กํ„ฐ๋ฅผ ์ƒ์„ฑํ•˜์ง€ ์•Š๋Š”๋‹ค.
    • Do Not Spawn: ์ง€์ •๋œ ์œ„์น˜์—์„œ ์ถฉ๋Œ์ด ๋ฐœ์ƒํ•˜๋ฉด ์•กํ„ฐ๋ฅผ ์ƒ์„ฑํ•˜์ง€ ์•Š๋Š”๋‹ค.

์•กํ„ฐ ์ƒ์„ฑ

 

์ƒ์„ฑ๋  ์•กํ„ฐ์˜ ์œ„์น˜๋Š” ์ €์žฅํ•œ ์Šคํฐ ํฌ์ธํŠธ์—์„œ ๋žœ๋คํ•˜๊ฒŒ ํ•˜๋‚˜๋ฅผ ๋ฝ‘๋Š”๋‹ค. ๊ทธ ํ›„ ์ค‘๋ณต๋œ ์œ„์น˜๊ฐ€ ๋˜์ง€ ์•Š๋„๋ก ๋ฝ‘ํžŒ ์Šคํฐ ํฌ์ธํŠธ๋Š” ์ œ๊ฑฐํ•œ๋‹ค.

 

๋žœ๋คํ•œ ์Šคํฐ ํฌ์ธํŠธ ๊ฐ€์ ธ์˜ค๊ธฐ

 

2) C++ ๊ตฌํ˜„

UPROPERTY(EditDefaultsOnly)
TArray<TSubclassOf<AItem>> ItemClass;
#include "Kismet/GameplayStatics.h"

UGameplayStatics::GetAllActorsOfClassWithTag(GetWorld(), ASpawnPoint::StaticClass(), "Item", FoundActors);
for (AActor* a : FoundActors)
	ItemSpawnPoints.Add(Cast<ASpawnPoint>(a));

FActorSpawnParameters SpawnParams;
SpawnParams.SpawnCollisionHandlingOverride = ESpawnActorCollisionHandlingMethod::AdjustIfPossibleButAlwaysSpawn;
SpawnParams.TransformScaleMethod = ESpawnActorScaleMethod::MultiplyWithRoot;

TArray<FTransform> SpawnPoints;
for (ASpawnPoint* a : ItemSpawnPoints)
	SpawnPoints.Add(a->GetActorTransform());

int SpawnIndex = FMath::RandRange(0, SpawnPoints.Num() - 1);
AItem* Item = GetWorld()->SpawnActor<AItem>(ItemClass[0], SpawnPoints[SpawnIndex], SpawnParams);

SpawnPoints.RemoveAt(SpawnIndex);

 

2. Nav ๋‚ด ๋žœ๋ค ์œ„์น˜์— ์ƒ์„ฑํ•˜๊ธฐ

GetRandomReachablePointInRadius ๋…ธ๋“œ๋ฅผ ํ†ตํ•ด origin์—์„œ Radius ๋‚ด ๋„๋‹ฌํ•  ์ˆ˜ ์žˆ๋Š” ๋žœ๋คํ•œ ์œ„์น˜๋ฅผ ๊ฐ€์ ธ์˜ฌ ์ˆ˜ ์žˆ๋‹ค.

 

1) ๋ธ”๋ฃจํ”„๋ฆฐํŠธ ๊ตฌํ˜„

GetRandomReachablePointInRadius ๋…ธ๋“œ

 

2) C++ ๊ตฌํ˜„

FTransform AMachine::GetRandomTransform(float Radius)
{
    FNavLocation NavigationLocation;
    UNavigationSystemV1::GetNavigationSystem(GetWorld())->GetRandomReachablePointInRadius(EnemySpawnPoint->GetComponentLocation(), Radius, NavigationLocation);
    return UKismetMathLibrary::MakeTransform(NavigationLocation, UKismetMathLibrary::FindLookAtRotation(NavigationLocation, EnemySpawnPoint->GetComponentLocation()));
}

 

์ฐธ๊ณ 

https://www.youtube.com/watch?v=tf0BvCl11lE

์ €์ž‘์žํ‘œ์‹œ (์ƒˆ์ฐฝ์—ด๋ฆผ)
'๐Ÿ“ Game/โœ Unreal' ์นดํ…Œ๊ณ ๋ฆฌ์˜ ๋‹ค๋ฅธ ๊ธ€
  • [Unreal][C++] ๋ฐ”๋ผ๋ณด๋Š” Actor์—๊ฒŒ ํšจ๊ณผ ์ฃผ๊ธฐ
  • [Unreal][C++] ์บ๋ฆญํ„ฐ ์นด๋ฉ”๋ผ ๊ฐœ์„ 
  • [Unreal] ์ง€ํ˜• ๋งŒ๋“ค๊ธฐ (Light, Landscape, Water Plugin)
  • [Unreal] ํƒ€์ž„๋ผ์ธ(Timeline)
Blxxming
Blxxming
CS ์ง€์‹๊ณผ ๊ณต๋ถ€ํ•˜๋‹ค ๋ฐฐ์šด ๊ฒƒ, ๊ฒฝํ—˜ํ•œ ๊ฒƒ ๋“ฑ์„ ๊ธฐ๋กํ•˜๋Š” ๋ธ”๋กœ๊ทธ์ž…๋‹ˆ๋‹ค.
  • Blxxming
    ๐Ÿ’ก๋ฒˆ๋œฉ๐Ÿ’ก
    Blxxming
  • ์ „์ฒด
    ์˜ค๋Š˜
    ์–ด์ œ
  • ๊ณต์ง€์‚ฌํ•ญ

    • Tech Interview
    • ๐Ÿ“š Tech (246)
      • ๐Ÿ“ Computer Science (96)
        • โœ OS (12)
        • โœ Network & Web (10)
        • โœ Database (11)
        • โœ Data Structure (6)
        • โœ Algorithm (40)
        • โœ Design Pattern (9)
        • โœ Cloud Computing (3)
        • โœ (5)
      • ๐Ÿ“ Language (73)
        • โœ Language (6)
        • โœ C & C++ (11)
        • โœ C# (19)
        • โœ JAVA (37)
      • ๐Ÿ“ Game (43)
        • โœ Computer Graphics (2)
        • โœ Unity (14)
        • โœ Unreal (26)
        • โœ (1)
      • ๐Ÿ“ Book (34)
        • โœ Effective (3)
        • โœ Game Server (16)
        • โœ Clean Code (14)
        • โœ (1)
  • hELLOยท Designed By์ •์ƒ์šฐ.v4.10.0
Blxxming
[Unreal][C++] ์•กํ„ฐ ์ƒ์„ฑ
์ƒ๋‹จ์œผ๋กœ

ํ‹ฐ์Šคํ† ๋ฆฌํˆด๋ฐ”