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?
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
print(cf)
game.ReplicatedStorage:WaitForChild("RemoteEvent"):FireServer(cf)
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
print(cf.Position)
print(cf.LookVector)
game.ReplicatedStorage:WaitForChild("RemoteEvent"):FireServer(cf.Position, cf.LookVector)
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
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.
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