Extend a part to a point

The goal

I want to extend a part to a specific point in the world (e.g. mouse hit pos). This is done only on the client. Specifically, I am trying to extend a part on 1 axis to the end vector, like stretching your arm to a part.

The problem

It simply tends to miss the point you aim at. Sometimes, it may just extend on the Z axis.

The information

I have looked on the DevForum, but I can’t find much that is related to this.
I have mainly tried making the part extend with Size and making the CFrame look at the target, but it doesn’t always work correctly. The current code I am using is here:

-- Part being the part created in workspace.
-- Distance is the Magnitude between the character's arm and the mouse position.
TS:Create(Part, TweenInfo.new(.4, Enum.EasingStyle.Sine, Enum.EasingDirection.In, 0, true, 0), {Size = Vector3.new(1, Character.RightUpperArm.Size.Y, distance), Transparency = 1}):Play()
	TS:Create(Part, TweenInfo.new(.4, Enum.EasingStyle.Quad, Enum.EasingDirection.InOut, 0, true), {CFrame = Part.CFrame * CFrame.new(0, 0, -15)}):Play()

If you could spare a couple of minutes of your time to help, I would be grateful.

1 Like

I don’t fully know what you’re trying to achieve but it seems you want a part to expand its size to the point,
maybe you could do:

for i = 0, 1, 0.1 do
local oldSize = part.Size.Z
part.Size = part.Size:Lerp(Vector3.new(1, arm.Size.Y, distance), i)
part.CFrame *= CFrame.new(0,0,(part.Size.Z-oldSize)/2)
task.wait(0.01)
end

Typed without testing anything

I suppose I’ll clarify, but from reading this I doubt it would solve the problem, but I’ll test it anyway.

This makes the arm shoot backwards. I’ll invert it and see if I get a good result.

Managed to make this though the code is a bit messy

It works by pointing the arm in the mouses direction then creating a part that expands straight forward from where the mouse is pointing

local uis = game:GetService("UserInputService")
local mouse = game:GetService("Players").LocalPlayer:GetMouse()

local char = game.Players.LocalPlayer.Character or game.Players.LocalPlayer.CharacterAdded:Wait()

uis.InputBegan:Connect(function(input, gameProcessed)
	if input.KeyCode == Enum.KeyCode.F then
		-- makes the part and adds the weld
		local part = Instance.new('Part')
		local we = Instance.new("Weld", part)
		local RightArm = char["Right Arm"]
		part.Anchored = false
		part.CanCollide = false
		part.Massless = true
		part.Size = RightArm.Size
		we.Part0 = RightArm
		we.Part1 = part
		part.Parent = workspace
		
		-- points the arm to where the mouse it
		local armOffset = char.Torso.CFrame:Inverse() * RightArm.CFrame
		local armWeld = Instance.new("Weld")
		armWeld.Part0 = char.Torso
		armWeld.Part1 = RightArm
		armWeld.Parent = char
		local cframe = CFrame.new(char.Torso.Position, mouse.Hit.Position) * CFrame.Angles(math.pi/2, 0, 0)
		armWeld.C0 = armOffset * char.Torso.CFrame:toObjectSpace(cframe)
		
		-- expands it
		for i = 0, 1, 0.1 do
			local distance = (RightArm.Position - mouse.Hit.Position).Magnitude
			part.Size = part.Size:Lerp(Vector3.new(RightArm.Size.X, distance, RightArm.Size.Z), i)
			
			we.C1 = CFrame.new(0, part.Size.Y/2 - RightArm.Size.Y/2, 0)
			task.wait()
		end
	end
end)

Add a size limit so if they point to the void it goes till the limit then retracts back

2 Likes

Thank you, I’ll try this later when I wake up

Hmm…Suppose the CFrame of Torso is (5, 2, 5), now with CFrame:Inverse, it gets (-5,-2,-5), now you multiply it by RightArm’s CFrame, let’s say it’s CFrame is (6,2,5), so by multiplying, it gets (-30, -4, -25), but it’s so random, so I wanna know what does this code mean ?

Ima be honest I got no clue that was just a result of a 5 second google search, the power of google is unmatched. I copy and pasted the arm pointing to mouse location theirs most likely a better way on getting arm offset but I wasn’t looking for the best code just an example.

Tested it, it still seems to work variably; my solution works better