- Home /
The question is answered, right answer was accepted
I want to generate 2 stacks of 3 objects around a circle but I get 1 stack of 6
I have 2 functions which generate instances of objects around the attached object's transform:
public void GenerateChairs()
{
Vector3 position;
Vector3 orientation;
float angle = 0.0F;
for (int i = 0; i < Game.players.Count; ++i)
{
angle = i * Mathf.PI * 2.0F / Game.players.Count;
position = new Vector3(Mathf.Cos(angle) * 0.01F, transform.position.y, Mathf.Sin(angle) * 0.01F);
orientation = transform.position - position;
GameObject instanceOfChair = Instantiate(chair, position, Quaternion.LookRotation(orientation));
}
}
public void GeneratePlayers()
{
GameObject characterType;
Vector3 position;
Vector3 orientation;
int numberOfGeneratedCharacters = 0;
float angle = 0.0F;
while (numberOfGeneratedCharacters < Game.players.Count - 1)
{
characterType = Game.humanColor == 0 ? characterSlime : characterTurtle;
angle = - numberOfGeneratedCharacters * Mathf.PI * 2.0F / Game.players.Count;
position = new Vector3(Mathf.Cos(angle) * 3.0F, 2.5F, Mathf.Sin(angle) * 3.0F);
orientation = transform.position - position;
GameObject computerCharacter = Instantiate(characterType, position, Quaternion.LookRotation(orientation));
computerCharacter.name = "Player " + (numberOfGeneratedCharacters + 2).ToString();
Game.characters.Add(computerCharacter);
computerCharacter.transform.Rotate(-60.0F, 0, 0, Space.Self);
numberOfGeneratedCharacters++;
}
angle = - numberOfGeneratedCharacters * Mathf.PI * 2.0F / Game.players.Count;
characterType = Game.humanColor == 0 ? characterTurtle : characterSlime;
position = new Vector3(Mathf.Cos(angle) * 3.0F, 2.5F, Mathf.Sin(angle) * 3.0F);
orientation = transform.position - position;
GameObject humanCharacter = Instantiate(characterType, position, Quaternion.LookRotation(orientation));
humanCharacter.name = "Player 1";
Game.characters.Insert(0, humanCharacter);
humanCharacter.transform.Rotate(-60.0F, 0, 0, Space.Self);
}
Basically I want Game.players.Count characters rendered 2.5F above Game.player.Count chairs, but in the end I get a sequence of PLAYER - CHAIR - PLAYER - CHAIR in a hexagon around the table. What did I do wrong with the math?
Not sure how to fix that particular code (because of strange differences between the 2 calculations, like the negative angle used in the second function) but it looks to me like you're creating an potential for error as well as a maintenance burden by unnecessarily duplicating stuff.
So I have a couple of alternative approaches to suggest...
First - if you want them in the same positions then write a function that'll give you those positions, rather than trying to duplicate the code that generates them. eg List<Vector3> GetCharacterPositionsForNPlayers(int numPlayers)
(or it could return a List<Pose>
if you want to include rotations). Then use the output of that function to get the positions in each of your functions (modifying the y-coordinates as required).
Or, and this is probably what I'd do... create one of the sets first and then use their positions to set the positions of the other set.