How to render Part between two points?

Basically, I have a gun that shoots a laser when the player clicks and holds. I want the laser to come out of the gun and end where the mouse is positioned (or as close to the mouse as the max length allows).

The issue is I can’t get the laser to rotate or position correctly. It resizes fine, but any movement throws the player off the map, despite the entire tool being cancollide = false. Here is the code I currently have.

local Players = game:GetService("Players")

local tool = script.Parent
local Configuration = tool.Configuration

local Chamber = tool.Chamber
local Handle = tool.Handle
local Laser = tool.Laser


local function RenderLaser()
	local mouse = Players.LocalPlayer:GetMouse()
	
	local startPos = Chamber.Position
	local endPos = mouse.Hit.Position
	
	
	local length = math.min(Configuration.StudRange.Value, (startPos - endPos).Magnitude)
	local midPoint = (startPos + endPos) / 2
	
	Laser.Transparency = 0.5
	Laser.Size = Vector3.new(length, Laser.Size.Y, Laser.Size.Z)
	Laser.Position = midPoint
	Laser.CFrame = CFrame.new(midPoint, endPos)
		
end


local function ResetLaser()
	Laser.Size = Vector3.new(0.05, 0.05, 0.05)
	Laser.Transparency = 1
end


local render = false

tool.Activated:Connect(function()
	render = true
	
end)

tool.Deactivated:Connect(function()
	render = false
	
end)


while true do
	if render then
		RenderLaser()
		
	else
		ResetLaser()
		
	end
	
	task.wait()
	
end
1 Like

It would help to see how your tool is setup. My guess is that your Laser part is welded to the tool, which in turn gets welded to the player’s hand. If the Laser part is welded to the tool and either becoming the root of your character assembly automatically (because you resize to be big) or it’s anchored, you’re gonna get a free trip over the rainbow.

You can solve root priority issues by making sure your character’s HumanoidRootPart has a higher RootPriority. Laser part should be set to have Part.Massless = true regardless, so that it does not affect your character’s mass or center of mass.

IMO though, if you’re after a standard laser beam look, you’re probably better off using a non-physics instance to visualize the beam, like: LineHandleAdornment, CylinderHandleAdornment, or Beam.

2 Likes

Thank you for the suggestions, they are super helpful!

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