Grid sytem like plane crazy

Hello, So I was trying to make a grid system like Plane Crazy
image
(🔨Plane Crazy🔨 [UPDATE] - Roblox) but I have this weird bug
image

local function calculateItemLocation(last, final: boolean): CFrame
	local Raycast, NilRaycast, X, Y, Z
	local MaxRange = 100
	local FloorHeight = 0.5
	local CamCF = workspace.CurrentCamera.CFrame
	local Radius = hitbox.Size / 2; Radius = CFrame.Angles(math.rad(rotationX), math.rad(rotationY), math.rad(rotationZ)):VectorToWorldSpace(Radius); Radius = convertToPositive(Vector3.new(roundToIntIfClose(Radius.X, 0.1), roundToIntIfClose(Radius.Y, 0.1), roundToIntIfClose(Radius.Z, 0.1)))
	Y = calculateYPos(plot.Position.Y, plot.Size.Y, Radius.Y * 2, 1) + 0.5

	if isMobile then
		Raycast = Workspace:Raycast(CamCF.Position, CamCF.LookVector * rangeOfRay, raycastParams)
		NilRaycast = CamCF.Position + workspace.CurrentCamera.CFrame.LookVector * (MaxRange + plot.Size.X * 0.5 + plot.Size.Z * 0.5)
	else
		local unit = workspace.CurrentCamera:ScreenPointToRay(mouse.X, mouse.Y, 1)
		Raycast = Workspace:Raycast(unit.Origin, unit.Direction * rangeOfRay, raycastParams)
		NilRaycast = unit.Origin + unit.Direction * (MaxRange + plot.Size.X * 0.5 + plot.Size.Z * 0.5)
	end

	if Raycast then
		X,Z,Y = Raycast.Position.X, Raycast.Position.Z, math.clamp(calculateYPos(Raycast.Instance.Position.Y, Raycast.Instance.CFrame:VectorToWorldSpace(Raycast.Instance.Size).Y, Radius.Y * 2, Raycast.Normal.Y, Raycast.Position), plot.Position.Y + plot.Size.Y / 2 + Radius.Y, (plot.Position.Y + plot.Size.Y / 2) + maxHeight - Radius.Y)

		if Raycast.Normal == Vector3.new(0,0,-1) then
			Z -= (Radius.Z * 2 - GRID_UNIT)
		elseif Raycast.Normal == Vector3.new(-1,0,0) then
			X -= (Radius.X * 2 - GRID_UNIT)
		end
	else
		X,Z = NilRaycast.X, NilRaycast.Z
	end

	if moveByGrid then
		local rel = plot.CFrame:Inverse() * Vector3.new(X,0,Z); if Raycast then rel = plot.CFrame:Inverse() * Vector3.new(X + Raycast.Normal.X,0,Z + Raycast.Normal.Z) end
		
		local snappedRel = snapCFrame(rel) * CFrame.new(Radius.X, 0, Radius.Z)

		if not removePlotDependencies then
			snappedRel = bounds(snappedRel, Radius.X, Radius.Z)
		end
		

		if Raycast and Raycast.Normal.Z == -1 then
			snappedRel = CFrame.new(snappedRel.X, snappedRel.Y, snappedRel.Z + (Radius.Z * 2 % 2))
		elseif Raycast and Raycast.Normal.X == -1 then
			snappedRel = CFrame.new(snappedRel.X + (Radius.X * 2 % 2), snappedRel.Y, snappedRel.Z)
		elseif Raycast and GRID_UNIT == 1 and Raycast.Normal.Z == 1 then
			snappedRel = CFrame.new(snappedRel.X, snappedRel.Y, snappedRel.Z - 1)
		elseif Raycast and GRID_UNIT == 1 and Raycast.Normal.X == 1 then
			snappedRel = CFrame.new(snappedRel.X - 1, snappedRel.Y, snappedRel.Z)
		end
		
		
		X = (plot.CFrame * snappedRel).Position.X
		Z = (plot.CFrame * snappedRel).Position.Z
	end

	if final then
		return CFrame.new(X,Y,Z) * CFrame.Angles(math.rad(rotationX), math.rad(rotationY), math.rad(rotationZ))
	end

	local Output = CFrame.new(X,Y,Z) * CFrame.Angles(math.rad(rotationX), math.rad(rotationY), math.rad(rotationZ))
	
	return Output * calcAngle(hitbox.CFrame, Output)
end
1 Like

It could be that your using math.ceil to round the y position meaning it goes 1 block higher. If you are maybe replace it with math.floor instead?

2 Likes