Why does this line trace code work and how can I port it to c++

For this I am using the character model from the first person template and I’ve slightly modified the gun and projectile.

I got the line trace portion of the code online and I understand most of it except for the camera manager part. Why use get player camera manager to get the player’s location? Why not the player controller? Also, why does GetActorLocation give me the exact center of the screen, where my cross hair is? I tried moving the camera and mesh around but the trace still goes towards the center. Why isn’t it affected by the positions and sizes of the mesh components?

Lastly, I am trying to make a class called Weapon (which is derived form Actor) and two derived classes: projectile and hit scan. Both will need the line trace so I want to put it in the Weapon class. But I am having trouble figuring out how to use the get player camera manager and line trace function.

In the APlayerCameraManager there is no GetActorLocation and geActortForwardVector so how do I get both of these values in c++.

For the trace, would this code be correct?

UWorld::LineTraceSingleByChannel(...)

The player camera manager is in charge of your camera.

As such using the camera managers location / forward vector will always return the location and forward vector of the camera that is in use. Which is equal to the center of your screen and directly forward (aka where your crosshair points at).

And… did you actually try to use “GetActorLocation” on APlayerCameraManager?

A stands for Actor. So without looking at the documentation I can tell you that APlayerCameraManager inherits from AActor and should therefore absolutely implement “GetActorLocation”. Since it’s parent (AActor) does.

If this does not yield the results you want / if you need the currently used camera itself, you can also use “GetViewTarget” which will return you the Actor that’s currently used as camera.