I’m trying to make a building system, and I am using raycast normals to offset the building part. The problem is, when the object is rotated, it gets all messed up.
This is how it looks like unrotated, nothing wrong
local mouseray = mouse.UnitRay
local CastRay = Ray.new(mouseray.Origin, mouseray.Direction * 1000)
local hit, position, normal = workspace:FindPartOnRayWithIgnoreList(CastRay, ignorelist)
local offset = Functions.getoffsetbynormal(Yrot, viewbuild.PrimaryPart, normal)
posX, posY, posZ = Functions.calculatePos(position, posX, posY, posZ, viewbuild)
local gridpos = Vector3.new(posX,posY,posZ)
viewbuild:PivotTo(CFrame.new(gridpos + ((offset * .5) * normal))*CFrame.Angles(viewbuild.PrimaryPart.Orientation.X, 0, viewbuild.PrimaryPart.Orientation.Z))
viewbuild:PivotTo(CFrame.new(gridpos + ((offset * .5) * normal))*CFrame.Angles(viewbuild.PrimaryPart.Orientation.X, math.rad(viewbuild.PrimaryPart.Orientation.Y + Yrot*90), viewbuild.PrimaryPart.Orientation.Z))
and this is the normal calculator offsetter Module
module.calnum = function(num)
return num % 2 == 0
end
module.getoffsetbynormal = function(rotation, object, normal)
if module.calnum(rotation) then
return object.Size
else
return Vector3.new(object.Size.Z, object.Size.Y, object.Size.X)
end
end