• Godot Help
  • Solving my multi-select troops movement (godot 4)

Hey, in the game, I am having trouble calculating the correct movement for my multi-selected troops.
Currently, I have implemented the movement where troops would stand side by side in the end coordinate. They will align on the Z axis no matter which direction they're coming from:

However, instead, I would like them to know the direction they're coming from and align accordingly:

What would be a formula to calculate a direction and alignment like that?
Currently my logic is pretty dumb:

func moveSelectedSoldiers(raycast: Dictionary, soldierTarget = null):
	var coord = raycast.position
	var startZ = selectedUnits.size() / 2 # calculates the start Z pos from which to align
	var unitZPos = coord.z - startZ
	for idx in selectedUnits.size():
		var soldier = selectedUnits[idx]
		soldier.moveTo(Vector3(coord.x, coord.y, unitZPos))
		unitZPos = unitZPos + 1

Thank you in advance

    Instead of trying to calculate it automatically and make it too complicated why not have it so that you can rotate the formation with a key/button held down and deal with the alignment problem that way? It's a pretty standard feature in most modern multi-selection features these days. For this I would maybe create a temporary node at the selected position and have the presumed position nodes that your agents are moving to as children of that.

    AspireMint Thank you, I think what helps me most is understanding the direction from this:
    var look_dir = -$Soldiers.transform.basis.z