Having problems with raycast tracer

I’m working on a raycast gun with a beam for the tracer, I’m doing that by creating two parts one at the origin of the ray and one at the end, in each part there is 1 attachment and the beam connects to those attachments, but for some reason the end position attachment is not taking on the position of the end part and is way off target.

Blue is the raycast and red is the beam:

Screenshot 2024-04-14 133146

I’m not sure why this is happening, in my code below I clearly set the end attachments position to the end part’s position:

			local startPart = Instance.new("Part")
			startPart.Size = Vector3.new(0.2, 0.2, 0.2)
			startPart.Position = humanoidRootPart.Position
			startPart.Anchored = true
			startPart.CanCollide = false
			startPart.Material = Enum.Material.SmoothPlastic
			startPart.Color = Color3.new(1, 0, 0)
			startPart.Parent = workspace

			local endPart = Instance.new("Part")
			endPart.Size = Vector3.new(0.2, 0.2, 0.2)
			endPart.Position = raycastResult.Position
			endPart.Anchored = true
			endPart.CanCollide = false
			endPart.Material = Enum.Material.SmoothPlastic
			endPart.Color = Color3.new(1, 0, 0)
			endPart.Parent = workspace

			
			local startAttachment = Instance.new("Attachment")
			startAttachment.Parent = startPart

			local endAttachment = Instance.new("Attachment")
			endAttachment.Position = endPart.Position
			endAttachment.Parent = endPart

			
			local beam = Instance.new("Beam")
			beam.Attachment0 = startAttachment
			beam.Attachment1 = endAttachment
			beam.Width0 = 0.2
			beam.Width1 = 0.2
			beam.FaceCamera = true
			beam.Color = ColorSequence.new(Color3.new(1, 0, 0))
			beam.Transparency = NumberSequence.new(0, 1)
			beam.Parent = workspace

Any help appreciated!

when parenting an attachment to a basepart, the attachment is automatically positioned at the parts center

			local startPart = Instance.new("Part")
			startPart.Size = Vector3.new(0.2, 0.2, 0.2)
			startPart.Position = humanoidRootPart.Position
			startPart.Anchored = true
			startPart.CanCollide = false
			startPart.Material = Enum.Material.SmoothPlastic
			startPart.Color = Color3.new(1, 0, 0)
			startPart.Parent = workspace

			local endPart = Instance.new("Part")
			endPart.Size = Vector3.new(0.2, 0.2, 0.2)
			endPart.Position = raycastResult.Position
			endPart.Anchored = true
			endPart.CanCollide = false
			endPart.Material = Enum.Material.SmoothPlastic
			endPart.Color = Color3.new(1, 0, 0)
			endPart.Parent = workspace

			
			local startAttachment = Instance.new("Attachment")
			startAttachment.Parent = startPart

			local endAttachment = Instance.new("Attachment")
			endAttachment.Parent = endPart

			
			local beam = Instance.new("Beam")
			beam.Attachment0 = startAttachment
			beam.Attachment1 = endAttachment
			beam.Width0 = 0.2
			beam.Width1 = 0.2
			beam.FaceCamera = true
			beam.Color = ColorSequence.new(Color3.new(1, 0, 0))
			beam.Transparency = NumberSequence.new(0, 1)
			beam.Parent = workspace

Do you know how I could create a bullet affect with the beam?

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.