So, I decided to make a placement system but my round function isn’t able to get the real normal
Quick explanation
My placement system uses the mouse target normal to add the half of the size multiplied by the normal, the normal can be 0, 0, 1
and only would add 1 positive of the Z face, and if it’s -1 it would add negative(so it’s removing, not adding)
Here’s the error
My code to calculate
I used this code snippet from @nicemike40 to round my normal.
local mouse = require(script.Util).Mouse
local item = Instance.new("Part", workspace)
item.Anchored = true
item.Parent = workspace
item.CanCollide = false
mouse.TargetFilter = {item}
local function sign(x)
if (x == 0) then return 0; end
if (x > 0) then return 1; end
return - 1;
end
function SnapToAxis(vec)
local lx = math.abs(vec.X)
local ly = math.abs(vec.Y)
local lz = math.abs(vec.Z)
if (lx > ly and lx > lz) then
return Vector3.new(sign(vec.X), 0, 0);
elseif (ly > lx and ly > lz) then
return Vector3.new(0, sign(vec.Y), 0);
else
return Vector3.new(0, 0, sign(vec.Z));
end
end
game:GetService("RunService").RenderStepped:Connect(function()
local target = mouse.Target
if not target then return end
local normal = mouse.TargetSurface
local snappedNormal = SnapToAxis(normal)
print("Not snapped: ", normal)
print("Snapped: ", snappedNormal)
local offsetCFrame = CFrame.Angles(0, math.rad(target.Orientation.Y), 0) * CFrame.new(
item.Size.X * snappedNormal.X / 2,
item.Size.Y * snappedNormal.Y / 2,
item.Size.Z * snappedNormal.Z / 2
)
item.CFrame = CFrame.new(mouse.Hit) * offsetCFrame
end)