팀프로젝트
LineTrace는 언리얼에서 직선 충돌 검사이다. 다른 엔진에서 보통 Raycast라고 부름
LineTrace
-> 시작점에서 끝점까지 보이지 않는 직선을 쏜다. 그 직선이 어떤 물체와 부딪히면, 무엇과 부딪혔는지, 어디에 부딪혔는지 정보를 돌려준다.
총알 판정, 조준점 판정, 상호작용 판정, 바닥 찾기, 벽 감지 등에 자주 사용
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;
}
FHitResult는 라인트레이스나 충돌 검사 결과를 담는 구조체이다.
GetWorld()->LineTraceSingleByChannel(
Hit, // 결과를 담을 변수
traceStart, // 라인트레이스 시작 위치
traceEnd, // 라인트레이스 끝 위치
ECC_Visibility // Collision Channel 중 하나
)
LineTrace
-> 직선 충돌 검사
Single
-> 여러 개가 아니라 첫 번째로 맞은 것 하나만
ByChannel
-> Collision Channel 기준으로 검사
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안에 있을때만 맞는다.
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)
{
// 데미지
}
그냥 단순히 사각형의 가로 세로를 정해서 빼주면 됨.
UNiagaraFunctionLibrary::SpawnSystemAtLocation(
this,
impactNiagaraSystem,
GetActorLocation()
);
떨어지는 메테오 NiagaraSystem Actor Spawn
const FVector2D randomCircle = FMath::RandPointInCircle(MeteorSpawnRadius);
반지름 안의 랜덤 2D 좌표를 만든다. 플레이어 주변 반지름 800짜리 원 안에서 랜덤한 X/Y 오프셋 하나를 뽑음
-> 이걸 플레이어 위치에 더하면 플레이어 주변 랜덤 위치가 된다.