Hi. I'm making a gun script, and using raycasting. So, for the ray, one part in the parameters is the facing, and for this, I use a part's CFrame.lookVector, as it's not associated with the player quite yet (so I can't use mouse.Hit.p). Everything's working okay, except for the fact that the ray is casting to the left, instead of to the front. I've made sure "FirePart" is facing the front, and the part's "Front" is correct, but the ray is still being cast to the left. Can somebody help me with getting the ray to be casted forward? Thanks.
Relevant Code:
local ray = Ray.new(script.Parent.FirePart.CFrame.p, (script.Parent.FirePart.CFrame.lookVector - script.Parent.FirePart.CFrame.p).unit*300) local hit, position = game.Workspace:FindPartOnRay(ray, nil, false, false) rayPart.Size = Vector3.new(projectileThicknessX, projectileThicknessY, distance) rayPart.CFrame = CFrame.new(position, script.Parent.FirePart.CFrame.p) * CFrame.new(0, 0, -distance/2)
(rayPart, all other previous variables defined before)
Your problem is with your logic in the definition of the Ray datatype. lookVector
is a unit vector that gives the vector direction in which a CFrame is facing, so there is no need to do any further computations with the FirePart's CFrame to determine the front facing direction:
local part = script.Parent.FirePart local ray = Ray.new(part.Position, part.CFrame.lookVector*300)