Is this lesson's Line Trace wrong?

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);
}

Following the code as provided within the lesson, you will receive the hit location based on where the crosshair is pointing to. Your code is not what is given within Ben’s instructions.

Are you setting CrosshairXLocation and CrosshairYLocation the same as Ben did? 0.5f and 0.3333f respectively. If so, your code looks like it is doing the same thing as his code.

Your WorldDirection == His LookDirection

Your WorldLocation came from DeprojectScreenPositionToWorld, which is the camera location. His StartLocation is the PlayerCameraManager Camera Location, which should be the same as his. But should this be the player location instead of the player camera location? Does it matter? If from the player location, then the math messes up, so I guess this is good enough.

All other aspects of your code appear to align with his pretty close. Try replacing your code with his, and see if you get the same results. I think it is key to look at your CrosshairXLocation and CrosshairYLocation variables, make sure they are either getting the actual location of the crosshair or set to his values, which should be similar if you followed the previous instructions earlier.

Privacy & Terms