Local X, Z Axis

I’m adding a grid snap to my object placement system.

Here’s the issue:

function CalculatePlace(mousePos, target, normal, rotation)
	if mousePos then
		local gridPos = Vector3.new(math.floor((mousePos.X / grid) + 0.5) * grid, math.floor((mousePos.Y / grid) + 0.5) * grid, math.floor((mousePos.Z / grid) + 0.5) * grid)
		local yOffset = self.Object.PrimaryPart.Size.Y / 2

		local objectCFrame = CFrame.new(gridPos, gridPos + normal) * 
			CFrame.Angles(math.rad(-90), math.rad(rotation), 0) *
			CFrame.new(0, yOffset, 0)

		return objectCFrame
	end
end

My code is rounding the position on all xyz axis. I need it to only snap the xz axis relative to the object’s position. Is there a way to get the local xz axis and to only round those. rounding the xz mousepos (a world position) isn’t a viable solution either because it breaks placing on walls.

Not very experienced with math or cframes here, I appreciate if you could help :slight_smile:

2 Likes

I have seen this problem before, I remember in school we learned about this thing called quaternions. It basically has something to do with the fourth dimension but basically the concept is it prevents euler grid lock by moving the part locally from a given already set rotation (if i remember)