Third person and LineTracing C++

Problem

I have a crosshair in the middle of the screen.

I need to create a lineTrace that passes straight through this crosshair.

As this is a third person game, I need the line trace to cast through the crosshair

Question

How do I do this

Code sample


#include "GameFramework/PlayerController.h"
#include "Engine/World.h"
#include "DrawDebugHelpers.h"
#include "Camera/PlayerCameraManager.h"
#include "GrabObjects.h"
#include "CollisionQueryParams.h"

#define OUT

//...

// Called every frame
void UGrabObjects::TickComponent(float DeltaTime, ELevelTick TickType, FActorComponentTickFunction* ThisTickFunction)
{
//...

FVector PlayerViewPointLocation;
FRotator PlayerViewPointRotation;

// Get player viewport
GetWorld()->GetFirstPlayerController()->GetPlayerViewPoint(
OUT PlayerViewPointLocation,
OUT PlayerViewPointRotation
);

// Draw a line from player showing reach
// PlayerViewPointRotation.Vector() is the unit vector
FVector LineTraceEnd = PlayerViewPointLocation + PlayerViewPointRotation.Vector() * Reach;

DrawDebugLine(
GetWorld(),
PlayerViewPointLocation,
LineTraceEnd,
FColor(0, 255, 0),
0.f,
0,
5.f
);

//...
}

$$$$

• You should edit your question to include a picture of your current output - if possible, you should also try to create an illustration of what you would like your output to be, as well. Mar 24 '20 at 10:00
• I'd like to remind users of our Code of Conduct. It's normal for users here to give you constructive edits and tips for clarifying/focusing your questions. This is important to get high-quality answers, and to be helpful to future readers who can learn your experience. Sometimes there will be misunderstandings, and you'll need to correct an edit, to bring it back to your intended meaning. In this case, it's best to add a clarifying comment to explain why what you need is different, and always treat your collaborators here with respect and kindness. Mar 25 '20 at 14:11
• Forgive me, it's my fault. Sorry guys Mar 26 '20 at 18:43
• I came across an old Game Developer Magazine article about your options here — see "Ready, Aim, Fire!" starting around page 15 (17 in the pdf). Aug 22 '20 at 12:13

You can try doing what you are now, but take the distance from the camera to the player, multiplied by the Camera forward vector to make it begin at the player. This should achieve what you want.

Example, may not be correct or work but hopefully conveys the idea:

float dist = FVector::Distance(PlayerViewPointLocation , GetWorld()->GetFirstPlayerController()->GetPawn()->GetActorLocation());
FVector StartPos = PlayerViewPointLocation + (GetWorld()->GetFirstPlayerController()->PlayerCameraManager->GetForwardVector() * dist);


Note: If you are familiar with Matrix transformations you can just use the Cameras Transform (inverse as you want to go from World to Local) to transform the players world space location into the cameras local. Then you can just use the X of the new vector.

• And what's the ending to this? Line tracing starts at StartPos and ends where? Thank you btw Mar 24 '20 at 4:05

Instead of using the predefined GetPlayerViewPoint() to set your location and rotation, you'll need to use this:

PlayerViewportLocation = GetWorld()->GetFirstPlayerController()->GetPawn()->GetActorLocation();
PlayerViewportRotation = GetWorld()->GetFirstPlayerController()->GetPawn()->GetActorRotation();
`

This will set your parameters from the location of the pawn itself and not the camera.

• Thank you, but this makes the line appear in front of the pawn, I want the line trace to appear in the hitbox in the middle of the screen Mar 19 '20 at 17:50