I am making a placement system for my sandbox game and I want the system to be kind of like scrap mechanic’s system of placing stuff but I can’t seem to be able to get a part to snap to the surfaceNormal of a part. I want to make it so you can rotate and turn the part and make it still snap to the surfaceNormal.
This is the code for calculating the position
game:GetService("RunService").RenderStepped:Connect(function()
local from_p = workspace.CurrentCamera.CoordinateFrame.p
local to_p = Mouse.Hit.p
local ignore = {Player.Character, Model}
local ray = Ray.new( from_p, (to_p - from_p).unit * 1000)
local newTarget, newHit, surfaceNormal = workspace:FindPartOnRayWithIgnoreList(ray, ignore)
local s = Model:GetExtentsSize();
local sX = s.X;
local sY = s.Y;
local sZ = s.Z;
local vector = Vector3.new(sX, sY, sZ)
if (rotB) then
sX = s.Z;
sY = s.X;
end;
if (rotZB) then
sX = s.Y;
sY = s.X;
sZ = s.Z;
end;
if (rotB) and (rotZB) then
sX = s.Z;
sY = s.X;
sZ = s.Y;
end;
local NormalOffset = ((surfaceNormal/2)*Vector3.new(sX,sY,sZ))
local CF = (CFrame.new(newHit) * CFrame.Angles(0,math.rad(rotation),math.rad(rotationZ))) + NormalOffset;
Model:SetPrimaryPartCFrame(CF)
end);
My code works, but some angles doens’t work. There must be a better way to do this. Any help would be greatly appreciated!