Raycast from camera center to where camera is looking

I read all the posts I could find on this topic but all of them are about raycasting from the camera center to the mouse position I know that I can get the camera center by dividing both x and y of the camera ViewportSize by two but how would I get the position of where the camera is looking?

I have something in mind which is using camera.CFrame.LookVector * distance but I can’t pass the camera from the client to the server using remote event, when I try using the camera on the server after I passed it from the client it just returns nil, how can I get where the camera is looking?

or at least be able to pass it properly to the server without returning nil using remote events?

You can’t pass it because the camera doesn’t replicate, what you can pass though is the look vector itself as it’s just a vector3.

1 Like

thats why i can’t Pass it, thx for letting me know but how would i solve this tho?

Well if you need to raycast from the camera on the server, you can pass the camera’s entire cframe through a remote event, you just can’t pass the camera itself:

print("On client:")
local cf = workspace.CurrentCamera.CFrame

I assume the difference is because of rounding error. Unfortunately not much you can do about that I don’t think.

However, it may be better to pass look vector and position differently rather than the whole cframe, that may fix the problem, though I can’t say for sure.

Edit: yeah I’d do that looks much more accurate:

print("On client:")
local cf = workspace.CurrentCamera.CFrame
game.ReplicatedStorage:WaitForChild("RemoteEvent"):FireServer(cf.Position, cf.LookVector)

1 Like

I did that and now I can access both camera position and orientation from the server, so I can get camera direction from camera.CFame.LookVector * Distance, but what about the position from where the ray will start casting?

from the example you have above there is no need to to pass camera.Position seperatlysince you can access it from cframe too, i tryed that and it works but the aim is off not in the center as expected

local RayOrigin = camCFrame.Position

local RayDirection = camCFrame.LookVector * BulletTravelDistance

Refer to my edit, I believe that should be of more assistance than passing the entire cframe. And more accurate

1 Like

After your edit, all I will add is look at what I posted, passing the cframe is quite inaccurate it seems, I’m not entirely sure why, but some of the values were no where near the cframe printed on the client. In the example where I passed both separately, it was the exact same.

1 Like

the aim is still Off, it seems to be near the top rather than in the middle and im not even sure about the y too so its just missed up. if you found a way to get the center with direction precisly let me know, thx