Make a part size to player’s mouse position

Hello, everyone. So i want to know how to make it so you put your mouse somewhere and in the humanoid root part, there would a line pointing out the magnitude between the mouse position and the root part. If it goes backwards it scales one way towards the mouse pos, and vice versa for the other part.

Hi basically you would first have to find the “3d position” of the mouse. Luckily, we don’t have to do the math for this as roblox has Mouse.UnitRay, and we can just find the end position by doing:

Mouse.UnitRay.Origin + Mouse.UnitRay.Direction * MaxRange

However, you may notice the part may go through other part’s which is not what we want, we want it to stop “growing” when it hits a surface. This can be achieved by raycasting and using the raycast.position as the end position.

Now we have the 3d mouse position we still need to make the part between these two points. To do that first we would have to make it so the part is rotated to “look at” the mouse.position from the HumanoidRoot.Position. Additionally, we would have to size the part to the distance between the Mouse position and the HumanoidRootPart.Position


local Part = Instance.new("Part")
Part.Parent = workspace
Part.Anchored = true
Part.CanCollide = false


local RunService = game:GetService("RunService")
local Player = game.Players.LocalPlayer
local Mouse = Player:GetMouse()

local IgnoreParams=  RaycastParams.new()
IgnoreParams.FilterDescendantsInstances = {Part}
IgnoreParams.FilterType = Enum.RaycastFilterType.Blacklist

local function Find3dMousePosition(MaxRange)
	local Result = workspace:Raycast(Mouse.UnitRay.Origin, Mouse.UnitRay.Direction * MaxRange, IgnoreParams)
	return 
		if Result then 
			Result.Position 
		else
			Mouse.UnitRay.Origin + Mouse.UnitRay.Direction * MaxRange
end

RunService.RenderStepped:Connect(function()
	if Player.Character == nil then return end
	if Player.Character:FindFirstChild("HumanoidRootPart") == nil then return end
	
	local End = Find3dMousePosition(20) -- change the 20 to whatever max range you want
	local Start = Player.Character.HumanoidRootPart.Position
	local Distance = (End - Start).Magnitude
	
	Part.CFrame = CFrame.lookAt(Start, End)	 * CFrame.new(0,0,-Distance/2)
	Part.Size = Vector3.new(1,1, Distance)
end)

Here is the code implementing the ideas I discussed above.

1 Like

The rotation isnt quite right. I want it to stick in the humanoid root part, and not go all the way up to the top of the y axis. Is it possible for it not to go up in the orientation

Hmm so basically you would have to set the Y Axis to something else, of the end position. If you want it to be a “straight” line not oriented in the y direction you would have to set the y Position of the end position to the y position of the start position. Like this:


local Part = Instance.new("Part")
Part.Parent = workspace
Part.Anchored = true
Part.CanCollide = false


local RunService = game:GetService("RunService")
local Player = game.Players.LocalPlayer
local Mouse = Player:GetMouse()

local IgnoreParams=  RaycastParams.new()
IgnoreParams.FilterDescendantsInstances = {Part}
IgnoreParams.FilterType = Enum.RaycastFilterType.Blacklist

local function Find3dMousePosition(MaxRange)
	local Result = workspace:Raycast(Mouse.UnitRay.Origin, Mouse.UnitRay.Direction * MaxRange, IgnoreParams)
	return 
		if Result then 
			Result.Position 
		else
			Mouse.UnitRay.Origin + Mouse.UnitRay.Direction * MaxRange
end

RunService.RenderStepped:Connect(function()	
	if Player.Character == nil then return end
	if Player.Character:FindFirstChild("HumanoidRootPart") == nil then return end
	
	local Start = Player.Character.HumanoidRootPart.Position
	local End = Find3dMousePosition(20)
	End = Vector3.new(End.X, Start.Y, End.Z) -- set End Positition.Y to the humanoidRootPart.Position.Y
	local Distance = (End - Start).Magnitude
	
	Part.CFrame = CFrame.lookAt(Start, End) * CFrame.new(0,0,-Distance/2)
	Part.Size = Vector3.new(1,1, Distance)
end)