During the challenge I used the FVector CameraWorldLocation
returned by DeprojectScreenPositionToWorld
instead of getting the Camera’s position.
As I understand it, the lesson’s code traces a line from the center of the screen instead of the crosshair.
Is this correct?
P.S.:
For reference, here’s my code:
bool ATankPlayerController::GetSightRayHitLocation(FVector& HitLocation) const { HitLocation = FVector(0.0f, 0.0f, 0.0f); FVector2D CrosshairLocation = GetCrosshairLocation(); FVector WorldDirection, WorldLocation; DeprojectScreenPositionToWorld(CrosshairLocation.X, CrosshairLocation.Y, WorldLocation, WorldDirection); if (GetLookVectorHitLocation(HitLocation, WorldLocation, WorldDirection)) { return true; } return false; } bool ATankPlayerController::GetLookVectorHitLocation( FVector& OutHitLocation, const FVector& WorldLocation, const FVector& WorldDirection ) const { FHitResult HitResult; if (GetWorld()->LineTraceSingleByChannel(HitResult, WorldLocation, WorldLocation + WorldDirection*LineTraceRange, ECC_Visibility)) { OutHitLocation = HitResult.Location; UE_LOG(LogTemp, Warning, TEXT("Hitting: %s"), *HitResult.GetActor()->GetName()); return true; } return false; } // Get the position of the crosshair (in pixels) FVector2D ATankPlayerController::GetCrosshairLocation() const { int32 ViewPortSizeX, ViewPortSizeY; GetWorld()->GetFirstPlayerController()->GetViewportSize(ViewPortSizeX, ViewPortSizeY); return FVector2D(ViewPortSizeX*CrosshairXLocation, ViewPortSizeY*CrosshairYLocation); }