for _,unit in pairs(selection) do
local relatedv3 = (CFrame.new(centerofselection) * CFrame.Angles(0, math.rad(averot), 0)):inverse() * unit.PrimaryPart.CFrame
unit.Humanoid:MoveTo(CFrame.new(centerofselection) * CFrame.Angles(0,-(centerofselection - targetpos).Unit.x,0):ToWorldSpace(relatedv3).p)
end
I want the army to maintain its formation, so I’m using the center of the army as the “pinwheel” which position is defined as centerofselection. relatedv3 being the units positional relation to the pinwheel.
Snippets like collision grouping were removed from the code for clarity.
function FaceDirection(model, dir, duration)
local hum = model:FindFirstChild("Humanoid")
local root = model:FindFirstChild("HumanoidRootPart")
local gyro = root:FindFirstChild("BodyGyro")
local now = os.clock()
local nt = 0
hum.AutoRotate = false
gyro.MaxTorque = Vector3.new(100000,100000,100000)
gyro.CFrame = CFrame.lookAt(Vector3.new(), dir)
gyro:SetAttribute("lastFace", now)
while nt < duration and gyro:GetAttribute("lastFace")==now do
nt+= task.wait()
end
if gyro:GetAttribute("lastFace")==now then
hum.AutoRotate = true;
gyro.MaxTorque = Vector3.new(0,0,0)
end
end
--- inside the loop
unit.Humanoid:MoveTo(CFrame.new(centerofselection) * CFrame.Angles(0,-(centerofselection - targetpos).Unit.x,0):ToWorldSpace(relatedv3).p)
task.spawn(function()
unit.Humanoid.MoveToFinished:Wait()
FaceDirection(unit, faceDir, 0.5)
end)
Your units will need a body gyro inside its root part, and its max torque set to Vector3.new(0,0,0) at first so it’s not active
OHH I just noticed the army wasn’t actually rotating towards the mouse, my bad
Construct a CFrame with the LookVector being the group’s original facing direction
local groupCFrame = CFrame.lookAt(centerofselection, centerofselection + faceDir)
Calculate the CFrame offset of each unit by doing:
local offset = groupCFrame:ToObjectSpace(unit.HumanoidRootPart.CFrame)
Apply the CFrame offset to the new “destination” groupCFrame. (This should work when the destination position is exactly the same as the original position, assuming their look vectors are different)
local destCFrame = CFrame.lookAt(targetPos, targetPos + (targetPos - groupCFrame.Position))
local unitCFrame = destCFrame * offset
unit.Humanoid:MoveTo(unitCFrame.Position)
To be honest, I’m not very well-versed with CFrame, this may work with some deviation or may not work at all. Let me know how it goes.
Actually the same premise as what I’m doing. Some suspicion has me thinking its a logic problem rather then an issue with the code, but thanks for the help.