Essentially, I’m creating NPC’s facing each other, and trying to align them to the center.
For a zone of 6, it displays as shown
For a zone of 18, it displays as shown
My attempt:
Initially, I set each character to the zone position, and then insert them to the table & call the function.
local function snapAllToZone(zone)
local zonePos = Vector3.new(zone.position.X, 0, zone.position.Z)
local leftCount = #zone.left
local rightCount = #zone.right
local totalCount = leftCount + rightCount
local gap = Vector3.new(15, 0, 0) -- gap between both sides (so they face each other)
local offset = Vector3.new(10, 0, 0) -- spacing between each npc
local totalOffset = (totalCount - 1) * offset.X
for i, npc in ipairs(zone.left) do
local highlight = Instance.new("Highlight")
local hrp = npc:WaitForChild("HumanoidRootPart")
local offsetFromCenter = ((i - 1) - (leftCount / 2)) * offset.X
hrp.Position = hrp.Position +
Vector3.new(-offsetFromCenter, 0, gap.X / 2)
highlight.Parent = npc
end
for i, npc in ipairs(zone.right) do
local hrp = npc:WaitForChild("HumanoidRootPart")
local offsetFromCenter = ((i - 1) - (rightCount / 2)) * offset.X
hrp.Position = hrp.Position +
Vector3.new(offsetFromCenter, 0, -gap.X / 2)
end
end
Would love to hear where my calculation(s) are wrong!