I’m wanting to lock the 2nd pole to the players grid. On vertical/horizontal lines it works fine, however, on diagonal walls, allows users to place outside of the grid.
So I tried the code below, but get this
While it locks to inside the grid, it allows angles <45/>45, which I do not want. The wall needs to lock to this diagonal at all times
local ClampMouse = require(script.Parent.ClampMouse)
local MouseRaycast = require(script.Parent.MouseRaycast)
local Round = require(script.Parent.Round)
local Constants = require(script.Parent.Parent.Constants)
local function Snap(vector1, vector2)
if vector2 then
local Angle = math.round(math.atan2(vector2.Z - vector1.Z, vector2.X - vector1.X) / math.rad(Constants.WALL_LOCK)) * math.rad(Constants.WALL_LOCK)
local Dist = math.abs((vector1 - vector2).Magnitude)
vector1 += Vector3.new(Dist * math.cos(Angle), 0, Dist * math.sin(Angle))
end
local Snapped = {}
for i, axis in ipairs{"X", "Y", "Z"} do
Snapped[i] = math.round(vector1[axis] / Constants.GRID_SIZE) * Constants.GRID_SIZE
end
return Vector3.new(table.unpack(Snapped))
end
return function(playersPlot, pole, lowerX, upperX, lowerZ, upperZ)
local MouseHit = MouseRaycast(Enum.RaycastFilterType.Blacklist, {playersPlot.Placing})
if not MouseHit then return end
local MouseClampedP = ClampMouse(MouseHit.Position, lowerX, upperX, lowerZ, upperZ)
if pole.Name == "Pole1" then
pole:PivotTo(
CFrame.new(
Round(
MouseClampedP + Vector3.new(0, pole.Size.X / 2, 0),
Constants.GRID_SIZE,
playersPlot.Base.Position.Y + (pole.Size.X / 2) + 0.05)
) * CFrame.Angles(0, 0, math.rad(90))
)
else -- Pole2 (needs to lock on a 45 degree)
local Pole1 = pole.Parent.Pole1
local NewCFrame = CFrame.new(
Round(
MouseClampedP + Vector3.new(0, pole.Size.X / 2, 0),
Constants.GRID_SIZE,
playersPlot.Base.Position.Y + (pole.Size.X / 2) + 0.05)
) * CFrame.Angles(0, 0, math.rad(90))
local SnappedPos = Snap(Pole1.Position, NewCFrame.Position) + Vector3.new(0, 1, 0)
print(SnappedPos)
local ClampPos = ClampMouse(SnappedPos, lowerX, upperX, lowerZ, upperZ)
pole:PivotTo(CFrame.new(ClampPos) * CFrame.fromEulerAnglesXYZ(0, 0, math.rad(90)))
end
end