Rotating an "army" twards mouse

I almost got it working, but rotation problems ended up being what stopped me.

Obviously, its not facing the mouse properly. This is probably due to how I’m determining the rotation in this bit, i just don’t know how to fix it.

CFrame.Angles(0,-(centerofselection - targetpos).Unit.x,0)

but heres the full code.

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

…not the physical humanoids rotating is the issue, that will be done when I code them actually moving twards the objective.

If I did do that, it would not maintain formation like I specified.

That code is also questionable.

Okay, what do you want to achieve exactly?

That code is what I understand what you mean with “Rotating an army towards mouse”

The code rotates the army towards the facing direction you specified. (faceDir)

I put task.spawn() because the code yields, so you’ll need to run it on a different thread to rotate all your units simultaneously/

Maybe this is a little bit easier for you to understand.

image

Your code does rotate the army, but it doesn’t maintain the formation. Should they have been moving, they would be “marching” in a diagonal.

Let me restate what i’m looking for: I want the army to face the mouse position while maintaining formation.

OHH I just noticed the army wasn’t actually rotating towards the mouse, my bad

  1. Construct a CFrame with the LookVector being the group’s original facing direction
local groupCFrame = CFrame.lookAt(centerofselection, centerofselection + faceDir)
  1. Calculate the CFrame offset of each unit by doing:
local offset = groupCFrame:ToObjectSpace(unit.HumanoidRootPart.CFrame)
  1. 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.