Hi there,i’ve been trying to create a laser sight for my drone and for that i need to cast a ray to the laser hole’s right vector.
But somehow this is happening
here’s the chunk of code that handles the ray:
game:GetService("RunService").Heartbeat:Connect(function()
local pcf = target.CFrame.p
jp = rs.Part0.CFrame:toWorldSpace(cf(0, 0.5, 0))
jp2 = ls.Part0.CFrame:toWorldSpace(cf(0, 0.5, 0))
cframe = cf()
cframe2 = cf()
cframe = cf(jp.p, pcf) * ca(0, 0, pi/2) * cf(0, 0, 0) * ca(rd(-90),rd(0),rd(90))
cframe2 = cf(jp2.p, pcf) * ca(0, 0, -pi/2) * cf(0, 0, 0) * ca(rd(-90),rd(0),rd(90))
local dist=(target.CFrame.p-tor.CFrame.p).Magnitude
if dist<maxdist then
lasl.Parent=workspace.Terrain
local targetl=(lsl.CFrame.RightVector*300-lsl.CFrame.p).unit*300
local rayl=rn(lsl.CFrame.p,targetl)
local lhit,lpos,lnorm=workspace:FindPartOnRayWithIgnoreList(rayl,{char,workspace.Terrain})
local ldist=(lsl.CFrame.p-lpos).magnitude
lasl.Size = v3(.1,.1, ldist)
lasl.CFrame=cf(lsl.CFrame.p,lpos)*cf(0,0,-ldist/2)*ca(0,0,0)
rs.C0 = rs.C0:lerp(rs.Part0.CFrame:toObjectSpace(cframe),.2)
ls.C0 = ls.C0:lerp(ls.Part0.CFrame:toObjectSpace(cframe2),.2)
else
lasl.Parent=nil
--lasr.Parent=nil
ls.C0=ls.C0:lerp(lsc0,.1)
rs.C0=rs.C0:lerp(rsc0,.1)
end
end)
anyone know what i did wrong?
Edit:
forgot to say i’m using some variables (v3 is Vector3.new,cf is CFrame.new,ca is CFrame.Angles,rn is Ray.new,rd is math.rad and pi is pi )