How to shoot at the center of the screen (Crosshair)

Hello.

I trace a DebugLine from the character in this way

// Storing the Location and Rotation of the Character
FRotator PlayerRotation = GetControlRotation();
FVector RotXVector = PlayerRotation.Vector();

// START PLUG
FVector StartPlug = FVector(GetActorLocation().X, GetActorLocation().Y, GetActorLocation().Z + 77);

// END PLUG 	
int32 distance = 5000; // Maximum distance of the trace
FVector EndPlug = (RotXVector * distance) + StartPlug;

// Draws the red debug line
DrawDebugLine(GetWorld(), StartPlug, EndPlug, FColor(255, 0, 0), true, -1, 0, 0.7f);

Notice I add 77 to the GetActorLocation().Z in the StartPlug because my Camera is at 77 in the Z axis.

The problem is that when I shoot:

And this is another client’s perspective

It’s not accurate. Why?

I add the Crosshair through Blueprints

The fact you can see the beam in your first image means the ray is not starting at the camera’s position. Assuming it’s an FPS (“PC” is your player controller):

 if ( PC && PC->PlayerCameraManager )
 {
     FVector StartPlug = PC->PlayerCameraManager->GetCameraLocation();
     FVector RotXVector = PC->PlayerCameraManager->GetActorForwardVector();
...

The rest of your code can stay the same

If not an FPS or the camera is at an offset from where you want the beam to come out of, then the answer is different so specify if that’s the case.

Having a Character, I can’t find any PlayerCameraManager

MyCharacter->GetController()->**PlayerCameraManager**

This won’t work

Cameras are associated with the player controllers they are attached to. That’s why you need that.

If the character you’re talking about is the player’s character and it has a player controller attached, then get it this way:

APlayerController* PC = Cast<APlayerController>(GetController());

You can get the player controller in single player by:

APlayerController *PC = GetWorld()->GetFirstPlayerController();

I don’t know if you’re a single player game or not. You can search for how to get the appropriate player controller in a multiplayer game. In multiplayer it also depends if you’re doing the operations in the client or the server. More here:

So, it’s a multiplayer game.
Any weapon has its own Owner Character (replicated with RepNotify), I use that Owner to grab the PlayerController and shoot the DebugLine. When I shoot this is what happens on Server and Client

I had no problems before shooting with the OwnerCharacter. Probably the PlayerController is the problem here?

The DebugLine is going to another way. Maybe I need to replicate something, like the camera position/rotation? I don’t know

The client image looks correct, except the dot is a little off-center, so I’d look into that.

I’m assuming it’s a listen server (not dedicated). The camera itself is not replicated. Try the following code to replace the above:

if ( PC)
{
    FVector StartPlug;
    FRotator PlrViewRot;
    PC->GetPlayerViewPoint(StartPlug, PlrViewRot);
    FVector RotXVector =PlrViewRot.Vector();
}

I resolved with GetBaseAimRotation

1 Like