I’m working on something that allows people to make custom shops, however I’ve run into an issue where when attempting to place something, the item goes into the corner of 4 squares instead of the centre. I’m terrible with math so how would I go about making it centred instead of in the corner?
Currently, I have this which snaps the object to the grid:
function module.ClampVector(self, vector: Vector3) -- basically clamps the mouse's hit position to the plot's size
return Vector3.new(
math.clamp(vector.X, -self.PlacementPlane.Size.X / 2, self.PlacementPlane.Size.X / 2),
vector.Y,
math.clamp(vector.Z, -self.PlacementPlane.Size.X / 2, self.PlacementPlane.Size.X / 2)
)
end
function module.SnapToGrid(self, vector: Vector3)
local gridSnap = self.GridSnap -- a number (5 in my case)
local modX, modZ = vector.X % gridSnap, vector.Z % gridSnap -- I'm thinking this is how I'd go about applying a 2.5 stud offset but idk what to do with this number
vector = self:ClampVector(vector)
return Vector3.new(
math.round(vector.X / gridSnap) * gridSnap,
math.round(vector.Y / gridSnap) * gridSnap,
math.round(vector.Z / gridSnap) * gridSnap
)
end
You can probably add X amount of studs to the returned Vector3
return Vector3.new(
math.round(vector.X / gridSnap) * gridSnap + 2, -- I added +2 to these 3
math.round(vector.Y / gridSnap) * gridSnap + 2, -- You can change it
math.round(vector.Z / gridSnap) * gridSnap + 2 -- to match what you need
)
While that technically works, it only advances it 2.5 studs which causes the part to advance to the next square even if the mouse isn’t inside of that square.