How would i be able to eliminate a offset on a building system?

Hello im trying to build a tile based building system and ive ran into a issue,
image
theres a offset between the part and the plot, and i have no idea how to fix it, ive looked in the dev forumand youtube and couldnt really find anything useful that could help me

heres my current snap function:

local function Snap(Position:Vector3,SnapAmount:number)
			local X = math.floor((Position.X+(SnapAmount/2))/SnapAmount)*SnapAmount -- Gets the position snapped to the snap amount on the x Axis
			local Y = math.floor((Position.Y+(SnapAmount/2))/SnapAmount)*SnapAmount -- Gets the position snapped to the snap amount on the y Axis
			local Z = math.floor((Position.Z+(SnapAmount/2))/SnapAmount)*SnapAmount -- Gets the position snapped to the snap amount on the z Axis
			local NewBlockPos = Vector3.new(math.round(X*1000)/1000,math.round(Y*1000)/1000,math.round(Z*1000)/1000) -- Removes floating point imprecision.
			return NewBlockPos

		end

Its kinda unclear what you are trying to do here, do you want the part to be in the corner of the plot?

1 Like

change all the math.floor to math.round

1 Like

yes exactly, i want the model to be on the corner of the plot

You could subtract half of the parts size from the final position to offset the part. The ball was to test it you can remove that

local function Snap(Position : Vector3, size : Vector3, SnapAmount : number)
	local X = math.floor((Position.X + (SnapAmount / 2)) / SnapAmount) * SnapAmount
	local Y = math.floor((Position.Y + (SnapAmount / 2)) / SnapAmount) * SnapAmount
	local Z = math.floor((Position.Z + (SnapAmount / 2)) / SnapAmount) * SnapAmount
	return Vector3.new(X - size.X / 2, Y - size.Y / 2, Z - size.Z / 2)
end

local ball = workspace.Ball
local block = script.Parent

local size = block.Size
local pos = ball.Position

block.Position = Snap(pos, size, 4)
1 Like

image
it sort of works but i had to replace math.floor whit math.round and theres still some offset

This might be because your plots size is uneven so you get this 1 stud difference from the edge. Else you could change your snap logic to start from the plots origin (corner of the plot) and work from there. I added a plotsOrigin parameter for the plots position. It will then subtract half of the plot so we can snap from the corner of the plot.

local function Snap(Position : Vector3, SnapAmount : number, PlotOrigin : Vector3)
	local X = math.floor((Position.X - PlotOrigin.X + (SnapAmount / 2)) / SnapAmount) * SnapAmount + PlotOrigin.X
	local Y = math.floor((Position.Y - PlotOrigin.Y + (SnapAmount / 2)) / SnapAmount) * SnapAmount + PlotOrigin.Y
	local Z = math.floor((Position.Z - PlotOrigin.Z + (SnapAmount / 2)) / SnapAmount) * SnapAmount + PlotOrigin.Z
	return Vector3.new(X, Y, Z)
end

task.wait(3)
local block = workspace.Spike
local plot = workspace.Plot
local ball = workspace.Ball
local ball2 = workspace.Snap
local ball3 = workspace.Final

local size = block.Size

local mouse = game.Players.LocalPlayer:GetMouse()
mouse.TargetFilter = ball

game:GetService("RunService").RenderStepped:Connect(function()
	local pos = mouse.Hit.Position
	local snapped = Snap(pos, 2, plot.Position)
	local final = Vector3.new(snapped.X + size.X / 2, snapped.Y + size.Y, snapped.Z + size.Z / 2)
	
	ball.Position = pos
	ball2.Position = snapped
	ball3.Position = final
	task.wait(1)
	block.Position = final
end)

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.