Hello im trying to build a tile based building system and ive ran into a issue,
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
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)