[C++] 라인트레이스

Woogle·2022년 11월 21일
0

언리얼 엔진 5

목록 보기
34/59

Line Trace

  • 직선을 쏴서 Block되는 액터를 반환한다.

📄 C++

✏️ TPSPlayer.h

	UPROPERTY(EditAnywhere)
	class UParticleSystem* bulletImpactFactory;	// 생성할 VFX 원본

	void LineShot();	// 라인트레이스 구현할 함수

✏️ TPSPlayer.cpp

void ATPSPlayer::LineShot()
{
	FHitResult hitInfo;
	FVector start = cameraComp->GetComponentLocation();	// 카메라 위치에서
	FVector end = start + cameraComp->GetForwardVector() * 300000;	// 카메라 앞300000 cm 까지
	FCollisionQueryParams params;
	params.AddIgnoredActor(this);	// 플레이어와 충돌 방지 (Ignore Self)

	if (GetWorld()->LineTraceSingleByChannel(hitInfo, start, end, ECollisionChannel::ECC_Visibility, params))
	{
		UGameplayStatics::SpawnEmitterAtLocation(GetWorld(), bulletImpactFactory, hitInfo.ImpactPoint);	// VFX 생성
	}
}

📄 Blueprint


Add Force

  • 액터에 물리적인 힘을 가한다.

📄 C++

✏️ TPSPlayer.cpp

void ATPSPlayer::LineShot()
{
	FHitResult hitInfo;
	FVector start = cameraComp->GetComponentLocation();	// 카메라 위치에서
	FVector end = start + cameraComp->GetForwardVector() * 300000;	// 카메라 앞300000 cm 까지
	FCollisionQueryParams params;
	params.AddIgnoredActor(this);	// 플레이어와 충돌 방지

	if (GetWorld()->LineTraceSingleByChannel(hitInfo, start, end, ECollisionChannel::ECC_Visibility, params))
	{
		UGameplayStatics::SpawnEmitterAtLocation(GetWorld(), bulletImpactFactory, hitInfo.ImpactPoint);
	}

	auto hitComp = hitInfo.GetComponent();	// HitResult에서 충돌한 컴포넌트 꺼냄
	if (hitComp && hitComp->IsSimulatingPhysics())	// 널 체크
	{
		FVector dir = (hitInfo.ImpactPoint - start).GetSafeNormal();	// 힘의 방향
		FVector force = dir * hitComp->GetMass() * 500000;	// F = m*a
		hitComp->AddForce(force);	// 힘을 가한다
	}
}
profile
노력하는 게임 개발자

0개의 댓글