- Home /
Unit Formation Aligns to a Reset Forward Position Rather Than Parent Object's Rotation
Hello and thank you for clicking here. I'm in some trouble. My game requires units to move around in formation, and this is the function that creates the formation depending on how many units you have.
public List<Transform> LineFormation(int numberInFormation)
{
foreach (Transform child in transform) //destroy prevoius list of gameobjects so we do not add twice
{
Destroy(child.gameObject);
}
List<Transform> soldierPositions = new List<Transform>();
for (int i = 0; i < numberInFormation; i++)
{
//create a list of children that can be accessed in the next loop
GameObject position = new GameObject("position " + i.ToString());
position.transform.parent = transform;
soldierPositions.Add(position.transform);
}
int count = 0;
float offsetX = offset;
float offsetZ = offset;
foreach (Transform soldier in soldierPositions)
{
Vector3 position = gameObject.transform.position;
if (count == rowInt) //if we have 7 soldiers in a row, start a new row
{
count = 0;
offsetZ += offset; //add a new row
}
//alternate between placements of positive x and negative x
if (count % 2 != 0) //number is odd
{
position.x -= offsetX; //offset once to left
}
else
{
if (count == 0) //first soldier is always set to middle
{
//do nothing so first soldier is naturally set to middle, reset the X offset
offsetX = offset;
}
else //every even numbered soldier goes to right of middle
{
position.x += offsetX;
offsetX += offset;
}
}
position.z -= offsetZ;
soldier.transform.position = position;
count++;
}
return soldierPositions;
}
So if the rotation on the parent object is not zero at the time of the formation creation( which will happen often in my game) you will always get a wonky looking formation.
here is an example where I rotated the Parent Object 90 degrees from 0 and sure enough the formation lined up 90 degrees off of the parent because they were aligning to zero
This is an example of how it looks when the parent transform's rotation is reset to 0 and how it should look no matter the rotation of the parent object at the call of my function.
If you read this far thank you, and looking forward to your ideas.
I guess I'm not sure exactly what you are asking... do you just want the archers to always line up behind the parent soldier? So in the top picture you want the archers to be on the top half of the screen because the parent is facing down?
sorry I see now that the screenshots were unclear. The parent object actually follows the player around, you can see it's rotation based on the axes but it isn't actually visible in game. Ideally I would like the row of archers to align with that parent object, so always facing in the direction of the blue axis.
Answer by jackmw94 · Jan 30, 2021 at 12:05 AM
I believe your problem is that you're using the soldiers' transform's "position" which is their global real-world space position, rather than their "localPosition" which is their position relative to their parent.
Change soldier.transform.position = position;
to soldier.transform.localPosition = position;
and you should be fine.
Yes I just had to reset the position but you've totally fixed the offset rotation issue thank you!
Your answer
Follow this Question
Related Questions
Simple script to move object doesn't work 1 Answer
How to transform local space to world GYRO space on ios? 0 Answers
How to change transform.position.x to go backwards at specific instance? 2 Answers
Vector3 math problem? 1 Answer
Get closest Vector3 position from a GameObject and two Transforms (and the line inbetween them) 2 Answers