Hi Guys, so I was currently tweaking a placement system, but it’s an error and it’s not working well. Basically I set the Y value to 0.5, but for some reason, it’s -9.5 when testing.
local part = Instance.new("Part", workspace)
local mouse = game.Players.LocalPlayer:GetMouse()
local RS = game:GetService("RunService")
local gridSize = 4
mouse.TargetFilter = part
function snapToGrid()
local x = math.floor(mouse.Hit.X/gridSize + 0.5) * gridSize
local y = 0.5
local z = math.floor(mouse.Hit.Z/gridSize + 0.5) * gridSize
return CFrame.new(x,y,z)
end
function convertToObjectSpace()
local base = workspace.Baseplate.CFrame
local objspace = snapToGrid():ToObjectSpace(base)
return CFrame.new(math.clamp(objspace.X, -workspace.Baseplate.Size.X/2, workspace.Baseplate.Size.X/2, 0.5, math.clamp(objspace.Z, -workspace.Baseplate.Size.Z/2, workspace.Baseplate.Size.Z/2))
end
RS.RenderStepped:Connect(function()
part.CFrame = workspace.Baseplate.CFrame * convertToObjectSpace()
end)
Is there any problem with the math I did, or anything else. Thanks!
The multiply function for CFrames adds to the current CFrame so you are adding 0.5 to what the CFrame currently was. Instead, you should set the CFrame to that value.
local part = Instance.new("Part", workspace)
local mouse = game.Players.LocalPlayer:GetMouse()
local RS = game:GetService("RunService")
local gridSize = 4
mouse.TargetFilter = part
function snapToGrid()
local x = math.floor(mouse.Hit.X/gridSize + 0.5) * gridSize
local y = 0.5
local z = math.floor(mouse.Hit.Z/gridSize + 0.5) * gridSize
return CFrame.new(x,y,z)
end
function convertToObjectSpace()
local base = workspace.Baseplate.CFrame
local objspace = snapToGrid():ToObjectSpace(base)
return Vector3.new(math.clamp(objspace.X, -workspace.Baseplate.Size.X/2, workspace.Baseplate.Size.X/2, 0.5, math.clamp(objspace.Z, -workspace.Baseplate.Size.Z/2, workspace.Baseplate.Size.Z/2)))
end
RS.RenderStepped:Connect(function()
part.Position = convertToObjectSpace()
end)
But instead of going to mouse position, it goes to opposite direction. I just want to know how to convert it back to World Space.
I revamped the whole script and fixed the problem -
local part = Instance.new("Part", workspace)
part.Anchored = true
local mouse = game.Players.LocalPlayer:GetMouse()
local RS = game:GetService("RunService")
local gridSize = 4
mouse.TargetFilter = part
function snapToGrid()
local x = math.floor(mouse.Hit.X/gridSize + 0.5) * gridSize
local y = 0.5
local z = math.floor(mouse.Hit.Z/gridSize + 0.5) * gridSize
return CFrame.new(x,y,z)
end
function convertToObjectSpace()
local base = workspace.Baseplate.CFrame
local objspace = base:ToObjectSpace(snapToGrid())
return Vector3.new(math.clamp(objspace.Position.X, -workspace.Baseplate.Size.X/2, workspace.Baseplate.Size.X/2), 0.5, math.clamp(objspace.Position.Z, -workspace.Baseplate.Size.Z/2, workspace.Baseplate.Size.Z/2))
end
mouse.Move:Connect(function()
local cframe = workspace.Baseplate.CFrame:ToWorldSpace(CFrame.new(convertToObjectSpace()))
part.CFrame = cframe + Vector3.new(0, 10.5, 0)
end)