Problem with normal surface on placement system

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)

Hi! What exactly are you trying to accomplish by smelling the normal to an axis and then rotating it again anyways?

What orientation should the placed part be?

I’m using your code snippet to round my normal surface for example from 0.6444, 0, 0 to 1, 0, 0 because I need to multiply the size from the object to add the offset

local offset = (size * normal / 2)

What orientation should the placed part be? Should it copy the orientation of the target part?

Yeah, it should copy the Rotation Y from the target

Nevermind, I solved it with this code snippet

local normal = mouse.TargetSurface
local relativeVector = target.CFrame:VectorToObjectSpace(
   normal
) -- this is the solution
1 Like

Good stuff. By the way, you probably don’t need the snap to axis function if you’re using local space anyways.

1 Like