[UE] Player 뒤쪽에 있는 적에게 Trace Hit 처리되어 총알이 뒤로 날아가는 (+Crosshair 빨간색으로 활성화) 문제 해결
현재 카메라에서 화면 중앙에 위치한 Crosshair 방향으로 Trace를 쏘아 Hit 처리를 하여 결과값을 바탕으로 Crosshair가 흰색에서 빨간색으로 변한다. 하지만 Camera와 Player 사이에 적이 위치한 경우도 Hit 처리가 되어 총알이 Muzzle 위치에서 뒤로 나가는 문제가 발생한다. Crosshair도 흰색에서 빨간색으로 활성화된다.
목차
Player 뒤쪽에 있는 적에게 Trace Hit 처리되어 총알이 뒤로 날아가는 (+Crosshair 빨간색으로 활성화) 문제 해결
문제상황
현재 카메라에서 화면 중앙에 위치한 Crosshair 방향으로 Trace를 쏘아 Hit 처리를 하여 결과값을 바탕으로 Crosshair가 흰색에서 빨간색으로 변한다.
하지만 Camera와 Player 사이에 적이 위치한 경우도 Hit 처리가 되어 총알이 Muzzle 위치에서 뒤로 나가는 문제가 발생한다. Crosshair도 흰색에서 빨간색으로 활성화된다.
해결방안
LineTraceSingleByChannel를 쏘는 위치를 Player 앞쪽으로 변경한다.
변경 전
void UCombatComponent::TraceUnderCrosshairs(FHitResult& TraceHitResult)
{
FVector2D ViewportSize;
if (GEngine && GEngine->GameViewport)
{
GEngine->GameViewport->GetViewportSize(ViewportSize);
}
FVector2D CrosshairLocation(ViewportSize.X / 2.0f, ViewportSize.Y / 2.0f); //화면 중앙
FVector CrosshairWorldPosition;
FVector CrosshairWorldDirection;
bool bScreenToWorld = UGameplayStatics::DeprojectScreenToWorld(
UGameplayStatics::GetPlayerController(this, 0),
CrosshairLocation,
CrosshairWorldPosition,
CrosshairWorldDirection
); // DeprojectScreenToWorld 성공하면 true, 실패하면 false
if (bScreenToWorld)
{
FVector Start = CrosshairWorldPosition;
FVector End = Start + CrosshairWorldDirection * TRACE_LENGTH; //TRACE_LENGTH는 내가 설정한 80000.0f
GetWorld()->LineTraceSingleByChannel(TraceHitResult, Start, End, ECollisionChannel::ECC_Visibility);
}
}
변경 후
void UCombatComponent::TraceUnderCrosshairs(FHitResult& TraceHitResult)
{
FVector2D ViewportSize;
if (GEngine && GEngine->GameViewport)
{
GEngine->GameViewport->GetViewportSize(ViewportSize);
}
FVector2D CrosshairLocation(ViewportSize.X / 2.0f, ViewportSize.Y / 2.0f); //화면 중앙
FVector CrosshairWorldPosition;
FVector CrosshairWorldDirection;
bool bScreenToWorld = UGameplayStatics::DeprojectScreenToWorld(
UGameplayStatics::GetPlayerController(this, 0),
CrosshairLocation,
CrosshairWorldPosition,
CrosshairWorldDirection
); // DeprojectScreenToWorld 성공하면 true, 실패하면 false
if (bScreenToWorld)
{
FVector Start = CrosshairWorldPosition;
if (Character.IsValid())
{
float DistanceToCharacter = (Character->GetActorLocation() - Start).Size();
Start += CrosshairWorldDirection * (DistanceToCharacter + 100.f);
}
FVector End = Start + CrosshairWorldDirection * TRACE_LENGTH; //TRACE_LENGTH는 내가 설정한 80000.0f
GetWorld()->LineTraceSingleByChannel(TraceHitResult, Start, End, ECollisionChannel::ECC_Visibility);
}
}
if (Character.IsValid())
{
float DistanceToCharacter = (Character->GetActorLocation() - Start).Size();
Start += CrosshairWorldDirection * (DistanceToCharacter + 100.f);
}
위의 코드 추가
실행화면
'⭐ Unreal Engine > UE Debugging Log' 카테고리의 다른 글
[UE] Visual Stduio 호환되지 않음, 로드 실패 (0) | 2023.11.13 |
---|---|
[UE5] 언리얼 엔진5.3 빌드하기 (UE build from source code) (0) | 2023.11.09 |
[UE5] 프로젝트에 Replication이 적용되지 않을때 확인할 사항 (0) | 2023.09.30 |
[Unreal] 카메라 충돌 문제해결 (0) | 2023.09.25 |
[Unreal] 애니메이션이 연속 재생되는 문제해결 (점프 실행 시 공중에서 팔닥거림 해결) (0) | 2023.09.18 |
댓글
이 글 공유하기
다른 글
-
[UE] Visual Stduio 호환되지 않음, 로드 실패
[UE] Visual Stduio 호환되지 않음, 로드 실패
2023.11.13 -
[UE5] 언리얼 엔진5.3 빌드하기 (UE build from source code)
[UE5] 언리얼 엔진5.3 빌드하기 (UE build from source code)
2023.11.09 -
[UE5] 프로젝트에 Replication이 적용되지 않을때 확인할 사항
[UE5] 프로젝트에 Replication이 적용되지 않을때 확인할 사항
2023.09.30 -
[Unreal] 카메라 충돌 문제해결
[Unreal] 카메라 충돌 문제해결
2023.09.25