Needing help with Calculating a Grid Based System with ROTATED and TILTED OBJECTS!

I’ve been working on a small project, and I added rotation and tilting, but it caused issues. The grid system I made was offset and so on. What I am trying to get is a grid formula that does not require an object’s face (NormalID).

My Script:

local replicatedStorage = game:GetService("ReplicatedStorage")
local runService = game:GetService("RunService")
local userInputs = game:GetService("UserInputService")

local eventsFolder = replicatedStorage:WaitForChild("Events")

local spawnBlock = eventsFolder:WaitForChild("SpawnBlock")

local player = game.Players.LocalPlayer
local mouse = player:GetMouse()

local placeholder = nil

local rotation = 0
local tilt = 0

local beingHeld = false

function getGridPos(x, y, z)
	return Vector3.new(math.round(x/4) * 4, math.round(y/4) * 4 + 2, math.round(z/4) * 4)
end

if workspace.Placeholders:FindFirstChild("Block") then
	workspace.Placeholders.Block:Destroy()
end

mouse.Button1Down:Connect(function()
	
	if beingHeld == true then
		
		if placeholder then

			if placeholder.Position then

				spawnBlock:FireServer(placeholder.CFrame)

			end

		end
		
	end
	
end)

script.Parent.Equipped:Connect(function()
	
	beingHeld = true
	
end)
script.Parent.Unequipped:Connect(function()
	
	beingHeld = false
	
end)

runService.Heartbeat:Connect(function()
	
	if mouse.Target then
		
		if mouse.Target.Parent == workspace.Blocks then

			if not placeholder then
				
				placeholder = replicatedStorage.Blocks:FindFirstChild(player.Object.Value):Clone()
				placeholder.Parent = workspace.Blocks
				
				placeholder.Size = Vector3.new(4, 4, 4)
				placeholder.Transparency = 0.5
				placeholder.Anchored = true
				placeholder.CanCollide = false
				
				mouse.TargetFilter = placeholder
				
			else
				
				if beingHeld == true then

					placeholder.Transparency = 0.5

				else

					placeholder.Transparency = 1

				end
				
			end
			placeholder.Color = player:WaitForChild("Color3Value").Value
			
			local targetPos = mouse.Target.Position
			--targetPos = getGridPos(targetPos.X, targetPos.Y, targetPos.Z)

			local targetSurface = mouse.TargetSurface.Name

			if targetSurface == "Top" then
				
				targetPos += Vector3.new(0, 4, 0)
				
			elseif targetSurface == "Bottom" then
				
				targetPos += Vector3.new(0, -4, 0)
				
			elseif targetSurface == "Front" then
				
				targetPos += Vector3.new(0, 0, -4)
				
			elseif targetSurface == "Back" then

				targetPos += Vector3.new(0, 0, 4)
				
			elseif targetSurface == "Left" then

				targetPos += Vector3.new(-4, 0, 0)
				
			elseif targetSurface == "Right" then

				targetPos += Vector3.new(4, 0, 0)
				
			end]]
			
			placeholder.CFrame = CFrame.new(targetPos) * CFrame.Angles(math.rad(tilt), math.rad(rotation), 0)
			
		end
		
	end
	
end)

userInputs.InputBegan:Connect(function(input, gP)
	
	if not gP then
		
		if input.KeyCode == Enum.KeyCode.R then
			
			rotation += 90
			
		elseif input.KeyCode == Enum.KeyCode.T then
			
			tilt += 90
			
		end
		
	end
	
end)

player:WaitForChild("Object"):GetPropertyChangedSignal("Value"):Connect(function()
	
	if placeholder then
		
		placeholder:Destroy()
		placeholder = nil
		
	end
	
end)
1 Like