I am trying to make a laser gun. The issue is I don’t know CFrame math. Rn the ray spawns like halfway through where I want it. I have tried multiplying it by its inverse but that didn’t change anything.
script.Parent.Equipped:Connect(function(mouse)
local char = game.Players.LocalPlayer.Character
mouse.Button1Down:Connect(function()
local rayParams = RaycastParams.new()
rayParams.FilterType = Enum.RaycastFilterType.Blacklist
rayParams.IgnoreWater = true
local raycast = workspace:Raycast(script.Parent.Handle.Position, (mouse.Hit.Position - char.PrimaryPart.Position).Unit * 50, rayParams)
local laser = Instance.new("Part", workspace)
laser.Size = Vector3.new(1, 1, (mouse.Hit.Position - char.PrimaryPart.Position).magnitude)
local forwardVector = (mouse.Hit.Position - char.PrimaryPart.Position).Unit
local upVector = Vector3.new(0, 1, 0)
-- Remember the right-hand rule
local rightVector = forwardVector:Cross(upVector)
local upVector2 = rightVector:Cross(forwardVector)
laser.CFrame = CFrame.fromMatrix(char.PrimaryPart.Position, rightVector, upVector2) * CFrame.new(0,0, -forwardVector/2)
end)
end)