Help Drawing Parts Between points

Hi,

I want to have a part drawn between the initial click position and where the mouse is released.

However it currently my code makes the part extend as much in the right direction as the wrong direction see here:

Draw2

I want it to stat where the mouse clicked on that point and only extend towards the mouse.
Any Suggestions of fixes will be greatly appreciated.

Thanks,

See my code below: (Im aware its only on a client script but this is intended)

local UserInput = game:GetService("UserInputService")
local Enabled = false
local Players = game:GetService("Players")
local Camera = workspace.CurrentCamera
local Mouse = Players.LocalPlayer:GetMouse()

local StartP


UserInput.InputBegan:Connect(function(input)
	if input.UserInputType == Enum.UserInputType.MouseButton1 then
		Enabled = true
		StartP = Mouse.Hit.p
		Part = Instance.new("Part")
		print("lmao")
	end
end)

UserInput.InputChanged:Connect(function(input)
	print(2)
	if input.UserInputType == Enum.UserInputType.MouseMovement then
		if Enabled == true then
			--if PP ~= nil then
			--	PP:Remove()
			--end
			local DV = (Mouse.Hit.p - StartP).Magnitude * 2
			Part.CFrame = CFrame.new(StartP,Mouse.Hit.p)
			Part.Size = Vector3.new(0.5,0.5,DV)		
			Part.Parent = workspace
			--PP = Part
			
			
			
		end
	end
end)

UserInput.InputEnded:Connect(function(input)
	if input.UserInputType == Enum.UserInputType.MouseButton1 then
		Enabled = false
	end
end)

.

1 Like

I believe, you put the *2 there because the part wasn’t reaching the mouse. But that’s perfectly normal because when you resize an object, it grows on both sides. To compensate for this, you have to move the object 1/2 of its length towards the mouse. In order to achieve this though, since the object doesn’t have a stable orientation, you’d need to use trigonometry.

2 Likes

Thanks for replying, i guess i could just /2 the end pos when constructing the CFrame too then

I’m not actually too familiar with CFrame, but I guess you can give it a try

1 Like

JollyGameCrazy’s answer is totally correct, you can get the CFrame operators to do all the complicated trigonometry for you though using the code below.

local DV = (Mouse.Hit.p - StartP).Magnitude
Part.CFrame = CFrame.new(StartP,Mouse.Hit.p) * CFrame.new(0, 0, -DV/2)
2 Likes