Objects rotator (rotate objects around a circle)

Hi, I’m working on a card game, and I need to create some sort of “object rotator” to show some cards. Maybe a picture will explain better what I’m trying to achieve:

[3137-rotator.jpg*|3137]

  • The numbered red boxes represent the cards (they are always facing the camera).
  • The big black circle is the Transform the cards are rotating around to
  • The yellow line is the circle on which the cards are constrained
  • The blue line is the radius of this circle

I tried implementing this stuff with the following code (which I found on the forum):

void DistributeOnCircle ()
{
	for (int i = 0; i < cards.Length; ++i)
	{
		float theta = (2 * Mathf.PI / cards.Length) * i;
		float x = Mathf.Cos(theta);
		float z = Mathf.Sin(theta);
		
		cards*.position = new Vector3(x, transform.position.y, z);*
  • }*
    }
    It kinda works, but clearly I did not understand how it exactly works, so I have a few questions:
    1) how can I set the radius in that code?
    2) how can I make one of the cards always appear in front of the camera (exactly like in the picture: card 1 is right in front of the camera; if I press a key and rotate the cards, card 2 (or card 5, in that case) should face the camera, too). I tried to rotate the Transform with iTween, setting the amount of the rotation to 360 / numberOfCards, but it’s not working as expected…
    Here’s another picture explaining what I need:
    [3148-rotator2.jpg|3148]*
    On the left, the current behaviour. On the right, what I need to do: one of the cards should always be aligned with the camera on the X axis, and all the others are spaced evenly on the circle.
  1. radius is radius
float x = radius * Mathf.Cos(theta);
float z = radius * Mathf.Sin(theta);
  1. offset is offset for cards, 2/5*PI for each card, cause 2PI is full round. you can shift it smoothly if needed
float theta = (2 * Mathf.PI / cards.Length) * i + offset;

Ok, I figured it out:

The radius is calculated just like Scroodge suggested, but I had to invert Sin for x and Cos for z, that works in my case and the card is perfectly centered to the camera’s x position. Also, I had to put -z instead of z in the Vector3 for the position to make the front card appear in fron of the camera and not in the opposite place. Here’s the full code:

float radius = 0.2f;
		
for (int i = 0; i < cards.Length; ++i)
{
	float theta = (2 * Mathf.PI / cards.Length) * i;

	float x = radius * Mathf.Sin(theta);
	float z = radius * Mathf.Cos(theta);
			
	cards*.position = new Vector3(x, transform.position.y, -z);*

}