Is this code okay?

I got the same results as Ben.

// Get World Location if linetrace through crosshair, true if hits land
bool AStarshipPlayerController::GetSightRayHitLocation(FVector& HitLocation) const // GetSightRayHitLocation
{
	// Find the crosshair position
	int32 ViewportSizeX, ViewportSizeY;
	GetViewportSize(ViewportSizeX, ViewportSizeY);
	auto ScreenLocation = FVector2D
	(
		OUT CrossHairXLocation * ViewportSizeX,
		OUT CrossHairYLocation * ViewportSizeY
	);

	FVector HitDirection; // OUT Parameter
	FVector WorldCameraLocation; // Throw away
	// "de-project" the screen position of the crosshair to the world direction
	auto WorldDirection = DeprojectScreenPositionToWorld(ScreenLocation.X, ScreenLocation.Y, WorldCameraLocation, HitDirection);
	UE_LOG(LogTemp, Warning, TEXT("World Direction: %s"), *HitDirection.ToString());

	// Line-trace along that look direction, and see what we hit (up to maximum range)

	return false;
}

Just curious.

Warm Regards,
K-B

Looks fine to me. Though I wouldn’t have the comment // Throw away as you can use that instead of what Ben does in the following lecture.

1 Like

This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.

Privacy & Terms