⭐ Unreal Engine/UE Debugging Log

[UE] Player 뒤쪽에 있는 적에게 Trace Hit 처리되어 총알이 뒤로 날아가는 (+Crosshair 빨간색으로 활성화) 문제 해결

Designerd 2023. 10. 3. 22:22

현재 카메라에서 화면 중앙에 위치한 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);
    		}

    위의 코드 추가

     


     

    실행화면