1
\$\begingroup\$

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

        //...
    }

```
\$\endgroup\$
4
  • \$\begingroup\$ 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. \$\endgroup\$
    – Natalo77
    Mar 24 '20 at 10:00
  • \$\begingroup\$ 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. \$\endgroup\$
    – DMGregory
    Mar 25 '20 at 14:11
  • \$\begingroup\$ Forgive me, it's my fault. Sorry guys \$\endgroup\$
    – Johnny Bra
    Mar 26 '20 at 18:43
  • \$\begingroup\$ 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). \$\endgroup\$
    – DMGregory
    Aug 22 '20 at 12:13
0
\$\begingroup\$

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.

\$\endgroup\$
1
  • \$\begingroup\$ And what's the ending to this? Line tracing starts at StartPos and ends where? Thank you btw \$\endgroup\$
    – Johnny Bra
    Mar 24 '20 at 4:05
0
\$\begingroup\$

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.

\$\endgroup\$
1
  • \$\begingroup\$ 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 \$\endgroup\$
    – Johnny Bra
    Mar 19 '20 at 17:50

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.