How would one add increments to this?

Hey devs!

I’m making a moving system and it’s turning out great, however,
I want to add increments because it’s way too smooth.

I’ve looked for solutions but no luck.
Here is my code (I’m using Handles btw) :

function onMove(face, distance)
	local step = distance - lastDistance
	local move = Vector3.FromNormalId(face) * step
		
	lastDistance = distance
	selected.CFrame = selected.CFrame * CFrame.new(move)
end

Any help would be greatly appreciated! :smiley:

A simple way would be to put a debounce in it.

I’ve tried it and it does works, but if I move it too fast, there will be a huge jump between point A to point B

Just guessing, but how about:

function onMove(face, distance)
	local step = distance - lastDistance
	if step > 2 then
    	local move = Vector3.FromNormalId(face) * step
    	lastDistance = distance
    	selected.CFrame = selected.CFrame * CFrame.new(move)
    end
end

[/quote]

You can use snap-to-grid function for that.

local function SnapToGrid(val, gridSize)
	return math.round(val / gridSize) * gridSize
end

Additionally, to fix this buggy movement you can do this.

selected.CFrame = originCF * CFrame.new(move)

Working example:

local handles = script.Parent
local selected = workspace.Part

local originCF = CFrame.identity
local moveIncrement = 1

local function SnapToGrid(val, gridSize)
	return math.round(val / gridSize) * gridSize
end

local function OnDragMove(face, distance)
	local step = SnapToGrid(distance, moveIncrement)
	local direction = Vector3.FromNormalId(face)
	
	local move = direction * step

	selected.CFrame = originCF * CFrame.new(move)
end

local function OnDragBegin()
	originCF = selected.CFrame
end

handles.MouseDrag:Connect(OnDragMove)
handles.MouseButton1Down:Connect(OnDragBegin)
1 Like

For more accuracy i recommend using modulo operator

local function GridPosition(pos,gridSize)
	return Vector3.new(pos.X - pos.X%gridSize,pos.Y - pos.Y%gridSize,pos.Z - pos.Z%gridSize)
end

Sorry for late response, but it works! Thanks for your post and I’m sure this well help others as well.

Will take this into consideration