- Home /
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:
![alt text][1]
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[i].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:
![alt text][2]
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]: /storage/temp/3137-rotator.jpg [2]: /storage/temp/3148-rotator2.jpg
Answer by AXXE78 · Aug 31, 2012 at 04:25 PM
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[i].position = new Vector3(x, transform.position.y, -z);
}
Answer by ScroodgeM · Aug 29, 2012 at 08:16 PM
1) radius is radius
float x = radius * Mathf.Cos(theta); float z = radius * Mathf.Sin(theta);
2) 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;
Thanks for your answer! I got the radius thing working, but I did not understand the second part about the offset...
theta - is start angle of first object to place. in your example it is zero (when i=0). so you need just to shift it to make start point in other place. 2PI (~6.28) is full round, so object will make a circle and returns to it's place. if you shift to 1/5 of round, each object will move to next place.
ah ok, now I see your point. But I'd rather rotate the main Transform to which the cards are attached to, not shifting the card themselves. In order to do that, I'm using iTween. What I want to achieve is having the "selected" card always in front of the camera. In the picture above, Card 1 is in front of the camera (it's the "selected" card). If i press a key, iTween rotates the main Transform of a certain amount, thus showing the next card in front of the camera (for example: if I rotate left, the next card is Card 2...if I rotate right, next one is Card 5, and so on). So the question is, how can I offset the cards AFTER they have been placed on the circle to make one of the cards exactly face the camera?
place cards using
cards[i].localPosition = new Vector3(x, heightOffset, z);
ins$$anonymous$$d of
cards[i].position = new Vector3(x, transform.position.y, z);
this will place cards relative to it's parent and will be rotated with parent. after that simply do with parent whatever you want.
why should I offset y position? it has to be the same as the parent. the picture above is a top view of the scene, I'm not interested in the Y value, I need to re-arrange the cards (or the parent) to make one of the cards face the camera, but I don't know how to do that.