Raycasting from a part to the center of the camera (Off-point)

Hello,

I already setup a raycast from a point to the center of the camera, but my only problem is that its not on point.

The raycast goes below the center of the screen just by a bit. I am projecting a raycast from RightHand to CFrame.lookVector, see below

local origin = player.Character.RightHand.Position
local direction= workspace.CurrentCamera.CFrame.LookVector*50

local raycastResult = workspace:Raycast(origin, direction, raycastParams)

Is there a way I can make it go on point without setting the origin to

local origin = CurrentCamera.CFrame.Position

I have searched everywhere for this solution, I will appreciate some feedback.

Thank you.

As you can see in the picture below

Screenshot 2022-09-26 183241

Would raycasting from the camera to the point work as well?

If so, use the link below to raycast from the center of the screen to whichever part you need.
Camera:ScreenPointToRay() ( | Roblox Creator Documentation

Thats what im trying to avoid, its because i want the projectile to spawn where the hand is and fly towards the center of the screen. However, If i set the origin to Camera.CFrame.Position, it will just spawn where the camera’s position is.

Using raycast from the camera will allow you to get a target point, and raycasting from the hand to the target point is quite easy.

I have no idea what your trying to say. Can you provide an example?

Yes, sorry. By raycasting from the screen you will get a target position (Vector3). Raycasting in the direction targetPosition - Hand.Position you will have a raycast from the hand to the point the camera is looking at

Okay now I understand, Ill try it out

I think the problem might be that the ray hit something on its way to the center of the camera. My “not very smart” solution is to cast a ray from the camera directly forwards to get the true “center of camera” and then let the ray go for the “true center of camera”. If u find this confusing I can write an example code for u :).

If you can provide one, that will be great

local cameraraycastthatisforreferencelol = workspace:Raycast(workspace.Currentworkspace.CurrentCamera.CFrame.Position, workspace.CurrentCamera.CFrame.LookVector*500)
local funnythingthatdemonstratestherealcameracenterpoint = cameraraycastthatisforreferencelol.Position

--your actual cast here lol
local origin = player.Character.RightHand.Position
local direction=  funnythingthatdemonstratestherealcameracenterpoint

local raycastResult = workspace:Raycast(origin, direction, raycastParams)

also I offer you an alternative solution if u are using shift lock switch (only works on local script):

local mouse = player:getmouse()

local origin = player.Character.RightHand.Position
local direction=  mouse.hit

local raycastResult = workspace:Raycast(origin, direction, raycastParams)

Okay so I understood where you came from and came with a solution.

I fired 2 raycasts, one from Origin of the Camera to the direction of the Camera lookVector. I took the true position and just made a unit from true position to the right hand.

1 Like

I believe @Mustroomf worded it better, but how did this work for you?