In a shooter I'm making, the player is forced into Shift Lock and I made a script so the player's arm (that holds the weapon) and head move up and down with the cursor.
To make my gun fire, I attempted to raycast starting from the "Head" of the gun (the front), but the problem is always that it's never in the angle I want. The X and Z are always perfectly aligned with the gun, but I can never get the ray to have the same angle as the gun's head.
This is what I've been trying:
local tlv = script.Parent.Parent.Torso.CFrame.lookVector local hlv = script.Parent.Head.CFrame local hit, position, normal = workspace:FindPartOnRay( Ray.new( script.Parent.Head.CFrame.p, Vector3.new(tlv.x * 1000, hlv.p.y, tlv.z * 1000) ), script.Parent.Parent ) local beam = Instance.new("Part", workspace) beam.BrickColor = BrickColor.new("Bright red") beam.FormFactor = "Custom" beam.Material = "Neon" beam.Transparency = 0.25 beam.Anchored = true beam.Locked = true beam.CanCollide = false local distance = (script.Parent.Handle.CFrame.p - position).magnitude beam.Size = Vector3.new(0.3, 0.3, distance) beam.CFrame = CFrame.new(script.Parent.Handle.CFrame.p, position) * CFrame.new(0, 0, -distance / 2)
The beam is copied from Roblox's tutorial on making a Laser Gun, and is just so I can see where the ray fires. I need to avoid using LocalScripts for this solution as well.
The beam is always a straight line starting from the head of the gun, and doesn't change its angle with the player.
By somehow starting the raycast at the part, I can avoid this problem.
How would I do this?