Hello. I’m making a top-down game where “minions” can be spawned around and moved with the player. No matter what I try, though, I’m stuck when attempting to arrange said minions in a thickening box formation around the player. I’ve attached some code to show how I perform a “rank and file” formation in front of the player. I already have ways to change the formation, but I can’t figure out how to calculate the minion’s coordinates offset from the player for the box formation. Is there any way to do this?
EDIT: Here is a rough idea of how I imagined the minion order (imagine they’re in a grid). I’m not sure if the third layer is a good pattern, so I’m open to modifications.
public int myID;
public float x = 0;
public float z = 0;
void AssignID (int minionID) { // called from another class
myID = minionID; // starts at zero, and is incremented for each new minion
}
void FormUp () {
if (myID % 2 == 0) { //go left
x = 0.5f * (1 + myID % 10);//rank and file formation
z = 1 * (1 + myID / 10);
} else { //go right
x = 0.5f * (myID % 10);//rank and file formation
z = 1 * (1 + myID / 10);
}
}
void Update {
if (myID % 2 == 0) { // go left
// offset the position of the minion from the player's position
// "movement" is a class that deals with the player's movement and...
// ...position on the screen
placement = new Vector3(movement.myX - x, 0, movement.myZ + z);
iTween.MoveUpdate(gameObject, placement, 0.4f);
} else { // go right
placement = new Vector3(movement.myX + x, 0, movement.myZ + z);
iTween.MoveUpdate(gameObject, placement, 0.4f);
}
}