2026-05-20 LineTarce, LineTraceSingleByChannel, NiagaraSystemActorSpawn, RandPointINCircle

조범근·2026년 5월 20일

TIL

목록 보기
62/83

C++ Week 12

Study

팀프로젝트



Today I Learned


UnrealC++

1. LineTrace

LineTrace는 언리얼에서 직선 충돌 검사이다. 다른 엔진에서 보통 Raycast라고 부름

LineTrace
-> 시작점에서 끝점까지 보이지 않는 직선을 쏜다. 그 직선이 어떤 물체와 부딪히면, 무엇과 부딪혔는지, 어디에 부딪혔는지 정보를 돌려준다.

총알 판정, 조준점 판정, 상호작용 판정, 바닥 찾기, 벽 감지 등에 자주 사용


1-1. LineTarce flow

FHitResult Hit;

const FVector traceStart = spawnLocation + FVector(0, 0, 1000.0f);
const FVector traceEnd = spawnLocation - FVector(0, 0, 3000.0f);

if (GetWorld()->LineTraceSingleByChannel(Hit, traceStart, traceEnd, ECC_Visibility))
{
    spawnLocation = Hit.ImpactPoint;
}
  1. 충돌 결과를 담을 FHitResult Hit 변수를 만든다.
  2. traceStart를 spawnLocation보다 Z +1000 위로 잡는다.
  3. traceEnd를 spawnLocation보다 Z -3000 아래로 잡는다.
  4. traceStart에서 traceEnd까지 직선을 쏜다.
  5. 그 직선이 Visibility 채널을 Block하는 물체와 부딪히면 true를 반환한다.
  6. 충돌 정보가 Hit 안에 채워진다.
  7. Hit.ImpactPoint를 spawnLocation으로 사용한다.

FHitResult Hit

FHitResult는 라인트레이스나 충돌 검사 결과를 담는 구조체이다.

  1. Hit.ImpactPoint -> 실제로 충돌한 표면 지점. 라인트레이스가 바닥과 부딪힌 정확한 위치.
  2. Hit.Actor -> 라인트레이스에 맞은 Actor
  3. Hit.Component -> 맞은 Actor 안의 실제 Component
  4. Hit.ImpactNormal -> 충돌한 표면이 바라보는 방향. 방향벡터를 반환
  5. Hit.Distance -> Trace 시작점에서 충돌 지점까지의 거리

LineTraceSingleByChannel

GetWorld()->LineTraceSingleByChannel(
    Hit, // 결과를 담을 변수
    traceStart, // 라인트레이스 시작 위치
    traceEnd, // 라인트레이스 끝 위치
    ECC_Visibility // Collision Channel 중 하나
)

LineTrace
-> 직선 충돌 검사

Single
-> 여러 개가 아니라 첫 번째로 맞은 것 하나만

ByChannel
-> Collision Channel 기준으로 검사




2. 거리 기반 범위 판정

const float Distance = FVector::Dist(GetActorLocation(), PlayerPawn->GetActorLocation());
if (Distance > impactRadius) return;

FVector::Dist(A, B)는 두 위치 사이의 거리를 구한다 단순히 y값을 빼고 구하고싶으면 Dist2D

if(distance > impactRadius) return;
impactRadius가 500이라면, 플레이어가 메테오 중심에서 500안에 있을때만 맞는다.

2-1. 사각형 판정은 어떻게 계산할까

const float HalfWidth = 500.0f;
const float HalfHeight = 300.0f;

const FVector Center = GetActorLocation();
const FVector Target = PlayerPawn->GetActorLocation();
const FVector Delta = Target - Center;

if (FMath::Abs(Delta.X) <= HalfWidth &&
    FMath::Abs(Delta.Y) <= HalfHeight)
{
    // 데미지
}

그냥 단순히 사각형의 가로 세로를 정해서 빼주면 됨.




3. Niagara System Spawn

3-1. Niagara System Actor Spawn

UNiagaraFunctionLibrary::SpawnSystemAtLocation(
    this,
    impactNiagaraSystem,
    GetActorLocation()
);

떨어지는 메테오 NiagaraSystem Actor Spawn


3-2. RandPointINCircle

const FVector2D randomCircle = FMath::RandPointInCircle(MeteorSpawnRadius);

반지름 안의 랜덤 2D 좌표를 만든다. 플레이어 주변 반지름 800짜리 원 안에서 랜덤한 X/Y 오프셋 하나를 뽑음

-> 이걸 플레이어 위치에 더하면 플레이어 주변 랜덤 위치가 된다.

0개의 댓글