Grid placement system isn't alligned

Hello,
I am having an issue with my placement system, when I try to put models on the grid, sometimes they aren’t alligned. I looked around for issues like this but couldn’t find a solution. Heres a video showing the issue:

Video

The first model isn’t alligned to the grid while the second one works just fine.

Code

		local cframe, size = model:GetBoundingBox()
		
		local xsize = size.X
		local is_even = xsize % 2 == 0
		local zsize = size.Z
		local is_even2 = zsize % 2 == 0
		
		local g = 1
		
		local function moveditem()
			local x = math.sign(mouse.Hit.Position.X)*((math.abs(mouse.Hit.Position.X) - math.abs(mouse.Hit.Position.X) % g) + (size.X % g))
			local z = math.sign(mouse.Hit.Position.Z)*((math.abs(mouse.Hit.Position.Z) - math.abs(mouse.Hit.Position.Z) % g) + (size.Z % g))
			local newlocation = Vector3.new(x, model.WorldPivot.Y, z)
			if is_even == true then
				if rotation == 90 or rotation == -90 then
					newlocation = Vector3.new(newlocation.X, newlocation.Y, newlocation.Z + 0.5)
				else
					newlocation = Vector3.new(newlocation.X + 0.5, newlocation.Y, newlocation.Z)
				end
			end
			if is_even2 == true then
				if rotation == 90 or rotation == -90 then
					newlocation = Vector3.new(newlocation.X - 0.5, newlocation.Y, newlocation.Z)
				else
					newlocation = Vector3.new(newlocation.X, newlocation.Y, newlocation.Z - 0.5)
				end
			end
			local newrotation = CFrame.Angles(0, math.rad(rotation), 0)
			local cframe =  CFrame.new(newlocation) * newrotation
			model:SetPrimaryPartCFrame(cframe)

Any help is appreciated!

In your case just manually change the model WorldPivot by 0.5 studs

Doesn’t work when changing it, and I would prefer not having to manually change every single model for a placement system to work.

Issue has been fixed after re-writing all the code

Function geteven:

local function geteven(size)
			
	local sizefloor = math.floor(size)
	local even = sizefloor % 2 == 0
			
	if even then
		even = 0.5
	else
		even = 0
	end
			
	return even
end


Code for moving:

local cframe, size = model:GetBoundingBox()
					
local evenx = geteven(size.X)
local evenz = geteven(size.Z)
					
local newposition = Vector3.new(math.round(mouse.Hit.Position.X) - evenx, model.WorldPivot.Y, math.round(mouse.Hit.Position.Z) - evenz)
local newrotation = CFrame.Angles(0, math.rad(rotation), 0)
local newcframe = CFrame.new(newposition) * newrotation
model:PivotTo(newcframe)
2 Likes

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