Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

CFrame.lookVector Not Using Front?

Asked by
u_g 90
8 years ago

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)

1 answer

Log in to vote
1
Answered by
BlackJPI 2658 Snack Break Moderation Voter Community Moderator
8 years ago

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)
0
Thank you so much, it works now. u_g 90 — 8y
0
No problem BlackJPI 2658 — 8y
Ad

Answer this question