Need help with math and positioning of parts

This is kinda a follow up to this topic: Set part center to mouse position? / Not make part leap over and I found a solution to it but it barely works and I think ive found a more efficient way,

				local Tween = TweenService:Create(game.Workspace.cam.gun.main.Slide, TweenInfo.new(.1, Enum.EasingStyle.Sine, Enum.EasingDirection.Out, 0, false, 0), {Position = Vector3.new(game.Workspace.cam.gun.main.Slide.normal.Position.X,game.Workspace.cam.gun.main.Slide.normal.Position.Y,mouse.Hit.Position.Z)	})
				Tween:Play()

That is my tween code currently and I found out if I put mouse.Hit.Position.Z + 1 at the end well it would kinda not do it. But now I cant figure out the math to actually make that “1” be calculated and not just a random number. Ive tried mouse.Hit.Position.Z / Slide.Position.Z but it didnt work.

1 Like

Hmm, tried it with my own quick slider implementation:

Handle is blue, track is black.

Here's code that has the behavior you don't want (and some other unrelated issues)
local player = game.Players.LocalPlayer
local mouse = player:GetMouse()

local exitCurrentState

function enterState(fEnterState, ...)
	if fExitCurrentState then
		fExitCurrentState()
	end
	fExitCurrentState = fEnterState(...)
end

function idleState()
	local c1; c1 = mouse.Button1Down:Connect(function()
		local target = mouse.Target
		if target and target.Parent.Name == "Slider" then
			enterState(draggingState, target.Parent)
		end
	end)
	
	return function()
		c1:Disconnect()
	end
end

function draggingState(slider)
	local track, handle = slider.Track, slider.Handle
	
	local function update()
		local mouseSliderPos = track.CFrame:PointToObjectSpace(mouse.Hit.p)
		local z = math.clamp(mouseSliderPos.Z, -track.Size.Z/2 + 1, track.Size.Z/2 - 1)
		slider.Handle.CFrame = slider.Track.CFrame:ToWorldSpace(CFrame.new(0, 0, z))
	end
	
	local c1; c1 = mouse.Button1Up:Connect(function() enterState(idleState) end)
	local c2; c2 = mouse.Move:Connect(update)
	
	update()
	return function()
		c1:Disconnect()
		c2:Disconnect()
	end
end

enterState(idleState)

And here’s a fixed version that stores the offset from the mouse to the handle when the dragging begins, and makes sure to always keep the handle at that same offset. That way it never jumps around, it only follows the movement of the mouse:

Animation5

Code
function draggingState(slider)
	local track, handle = slider.Track, slider.Handle
	local zOffset
	do
		local mouseSliderPos = track.CFrame:PointToObjectSpace(mouse.Hit.p)
		local handleSliderPos = track.CFrame:PointToObjectSpace(handle.Position)
		zOffset = handleSliderPos.Z - mouseSliderPos.Z
	end
	
	local function update()
		local mouseSliderPos = track.CFrame:PointToObjectSpace(mouse.Hit.p)
		local z = math.clamp(mouseSliderPos.Z + zOffset, -track.Size.Z/2 + 1, track.Size.Z/2 - 1)
		slider.Handle.CFrame = slider.Track.CFrame:ToWorldSpace(CFrame.new(0, 0, z))
	end
	
	local c1; c1 = mouse.Button1Up:Connect(function() enterState(idleState) end)
	local c2; c2 = mouse.Move:Connect(update)
	
	update()
	return function()
		c1:Disconnect()
		c2:Disconnect()
	end
end

Hope this helps somewhat

3 Likes

I love you. Thank you good sir.

1 Like