How do you make something go in the middle of the square instead of in the corner?

Hello!

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?

image

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

Any ideas as to how I’d approach this?

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.

https://gyazo.com/ba1523fc6f020625f238efddd22831cc

Like I said you can change the + 2. Try - 2.5

I know, I changed it to be half the grid size (2.5), but that still gives an undesired result.

Changing it to -2.5 would also cause the same thing to happen but inverse (back 2.5 studs instead of advance 2.5 studs)

Revert my changes and try to add the 2.5 in the Clamp instead

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) + 2.5,
		vector.Y + 2.5,
		math.clamp(vector.Z, -self.PlacementPlane.Size.X / 2, self.PlacementPlane.Size.X / 2) + 2.5
	)
end

Edit: actually i think it will have the same effect

It does, it has something to do with the rounding thing.

Hmm. Try this,

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 - 2.5, -self.PlacementPlane.Size.X / 2, self.PlacementPlane.Size.X / 2),
		vector.Y - 2.5,
		math.clamp(vector.Z - 2.5, -self.PlacementPlane.Size.X / 2, self.PlacementPlane.Size.X / 2)
	)
end
...
return Vector3.new(
	math.round(vector.X / gridSnap) * gridSnap + 2.5,
	math.round(vector.Y / gridSnap) * gridSnap + 2.5,
	math.round(vector.Z / gridSnap) * gridSnap + 2.5
)

What im thinking with this is, if you offset the “cursor” position, it will affect the clamping. Then you will add the vectors to center it.

1 Like