Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by AXXE78 · Aug 29, 2012 at 08:06 PM · rotationcircle

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

rotator.jpg (37.2 kB)
rotator2.jpg (64.8 kB)
Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

2 Replies

· Add your reply
  • Sort: 
avatar image
0
Best Answer

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);
             
             
 }
Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
0

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;
Comment
Add comment · Show 11 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image AXXE78 · Aug 29, 2012 at 08:39 PM 0
Share

Thanks for your answer! I got the radius thing working, but I did not understand the second part about the offset...

avatar image ScroodgeM · Aug 29, 2012 at 09:00 PM 0
Share

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.

avatar image AXXE78 · Aug 29, 2012 at 09:13 PM 0
Share

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?

avatar image ScroodgeM · Aug 29, 2012 at 09:17 PM 0
Share

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.

avatar image AXXE78 · Aug 29, 2012 at 09:37 PM 0
Share

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.

Show more comments

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

8 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Instantiate an image, equally in a circle and rotate it away from the center? 2 Answers

How do I rotate a circle of objects so that a specific object is in front of the camera? 2 Answers

Rotation circle to stop at a certain angle 1 Answer

Rigidbody movement along a Circular Path. 1 Answer

How do you orbit an object over a point on the map in a circle, with the object facing in the correct direction? 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges