Hello, I want some gameobjects to be alligned relative to a “leader” gameobject, so they can make a square. I store them in a list and change their position with an offset according to their list position. The problem is that the objects get alligned in world positions, not including the “leader” gameobject’s rotation. Any help?

Here’s a drawing of what I mean:

This is my script:

//more stuff...

var posToGo : Vector3 = hit.point;
				for (var l : int = 0; l < man.selectedUnits.Count; l++)
				{
					if(man.selectedUnits.IndexOf(gameObject) == 0)
					{
						offX = 0;
						offZ = 0;
					}
					else if(man.selectedUnits.IndexOf(gameObject) == 1)
					{
						offX = 2;
						offZ = 0;
					}
					if(man.selectedUnits.IndexOf(gameObject) == 2)
					{
						offX = -2;
						offZ = 0;
					}
					else if(man.selectedUnits.IndexOf(gameObject) == 3)
					{
						offX = 0;
						offZ = -2;
					}
					if(man.selectedUnits.IndexOf(gameObject) == 4)
					{
						offX = 2;
						offZ = -2;
					}
					else if(man.selectedUnits.IndexOf(gameObject) == 5)
					{
						offX = -2;
						offZ = -2;
					}
					if(man.selectedUnits.IndexOf(gameObject) == 6)
					{
						offX = 0;
						offZ = -4;
					}
					else if(man.selectedUnits.IndexOf(gameObject) == 7)
					{
						offX = 2;
						offZ = -4;
					}
					if(man.selectedUnits.IndexOf(gameObject) == 8)
					{
						offX = -2;
						offZ = -4;
					}
				}
				posToGo.x += offX;
				posToGo.z += offZ;
				StartCoroutine(Motion(posToGo));


//more stuff...
function Motion(pos : Vector3)
{
    while(Vector3.Distance(transform.position, pos) > 0.1)
    {
    	animation.CrossFade("run");
    	var targetRotation = Quaternion.LookRotation(pos - transform.position);
		transform.rotation = Quaternion.Slerp (transform.rotation, targetRotation, Time.deltaTime * 10);
	    transform.position = Vector3.MoveTowards(transform.position, pos, Time.deltaTime * movementSpeed);
	    if(Vector3.Distance(transform.position, pos) <= 0.1)
	    {
	    	animation.CrossFade("idle");
	    }
	    yield;
    }
}

By the image, I imagine that the leader is rotated. So you could rotate the leader to zero, then make the others as children of the leader, and then rotate back.
So the cheildren will follow the parent(leader) rotation.

You can use transform.right to get a local relative right vector from the transform then add that to the transform position to get world co-ords. Since your picture shows z,x coordinates you might have to play around to figure out which ones you need, and probably set up some variables to store them in for easier coding. If your z,x axis is correct you would have to have the camera under the x/z plane looking up so I wonder if it’s not quite accurate(probably just mixed up x & z that would make sense). But anyways you can get a relativeRight = leaderTransform.right; and relativeDown = leaderTransform.back; variable (or .forwards to keep using negative values) then use that to set positions like

posToGo = leaderTransform.position + (relativeRight * offX) + (relativeDown * offZ);

Hope that helps!

Old thread, but for those that stumble upon this and would like a quick way to have a position/direction relative to a transform; Unity has build-in functions called Transform.TransformPoint and Transform.TransformDirection. These will convert any vector3 position or direction to be relative to whatever transform you apply these to. In your case the TransformPoint will suffice.

Example
vector3 posOffset = new Vector3(1.0f, 0f, -1.0f); // this is your fixed relative position to the leader

vector3 posGoTo = leaderTransform.TransformPoint (posOffset); // this one you keep updating as the leader moves