My goal is to make a laser attachment that doesn’t phase through walls. Ive tried a few different things with some success, but once I mounted the laser to the gun things got weird! the end point was always in a certain direction and not moving with the player or the gun.
I’ve tried playing with which values I’m using to no success.
Below I will include all my code, since most of it is taken from the tutorials roblox provides.
Essentially, I want the raycast to go no more than 100 studs in the -X direction of the local position of AttachmentA.
local A1 = script.Parent:FindFirstChild("AttachmentA")
local A2 = script.Parent:FindFirstChild("AttachmentB")
local RS = game:GetService("RunService")
RS.Heartbeat:Connect(function()
local rayOrigin = A1.WorldPosition
local rayDirection = Vector3.new(A1.WorldPosition.X-100, A1.WorldPosition.Y, A1.WorldPosition.Z)
local raycastParams = RaycastParams.new()
raycastParams.FilterDescendantsInstances = {script.Parent.Parent.Parent.Parent}
raycastParams.FilterType = Enum.RaycastFilterType.Blacklist
local raycastResult = workspace:Raycast(rayOrigin, rayDirection, raycastParams)
if raycastResult then
A2.WorldPosition = raycastResult.Position
end
end)