Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 ellisju · Jul 04, 2019 at 08:27 PM · rotationobjectcircleplacement

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

I am building an inventory system that creates a ring GameObject containing one or more child 3d objects around the main camera, offset so that the the camera is closest to the object directly in front of it. I am looking to be able to take the position of the "active" object in the ring and rotate the ring around its X axis so that the active object is directly in front of the camera, but I cannot figure out the math. I assume the object's local position and radians will have something to do with it?

This is the code for populating my "ring" object;

 float y = mainCam.transform.position.y;
         for (int i = 0; i < wares.Count; i++) {
             float angle = i * Mathf.PI * 2f / wares.Count;
             Vector3 newPos = new Vector3(Mathf.Cos(angle) * radius, y, Mathf.Sin(angle) * radius);
             GameObject thing = wares[i].gameObject;
             GameObject go = Instantiate(thing, newPos, Quaternion.identity, ring.transform);
             inventory.Add(go);
         }
         ring.transform.position = mainCam.transform.position - transform.forward * (radius - 3);
         ring.transform.position = new Vector3(ring.transform.position.x, -.5f, ring.transform.position.z);
         ring.transform.SetParent(transform);

This creates an empty GameObject, "ring," takes the inventory objects I've populated, and places them equidistant from one another in a circle around the ring object, at a set radius, as children of the ring object. It then offsets the ring's position so that whatever object is directly in front of the camera is closer to it (and offsets the ring's y position just to center the object a little better.)

Now I Just want to be able to look at any one of the child objects and use its position to rotate the ring object so that particular child is directly in front of the camera. Can someone help me with the mathematical algorithm I would use to accomplish that?

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
1

Answer by Cornelis-de-Jager · Jul 04, 2019 at 09:48 PM

Hi @ellisju, below is a solution that I wrote. Its not been properly tested yet, but give it a go and let me know. You control the item select by calling the NextItem() or PreviousItem() functions. Or in this case pressing Q and E.

 public class CircleDisplay : MonoBehaviour {

     public List<Transform> obejctList;
     public int selectedIndex;
     public Transform center;
     public float radius = 5f;
     public float speed = 3f;
     
     void Start () {
         // First assume that everything is rotating around this object
         // to change center simple remove code below and assign manually
         center = transform;
         
     }
     public void Update () {
         
         // Arrange all the items in a circle
         PositionAll ();
         
         /* TEST CODE START: 1.0 */
         if (Input.GetKey(KeyCode.Q))
             PreviousItem ();
             
         if (Input.GetKey(KeyCode.E))
             NextItem ();    
         /* TEST CODE END: 1.0 */
     }
     
     void PositionAll () {
     
         // Count the number of objects
         var count = obejctList.Count();
         
         // Get the angle between each items
         var angle = (Mathf.PI * 2) / count;
         
         // Now position each object in the circle and add a index offset
         for (int i = 0; i < count; i ++ ){
             // Get the X and Y Position - Not the 'angle * selectedIndex' is what controls the offset
             var x = Mathf.Cos(angle * i + angle * selectedIndex) ;
             var y = Mathf.Sin(angle * i + angle * selectedIndex);
             
             var position = new Vector3(x, 0, y) * radius;
             
             obejctList.position = Vector3.MoveTowards (transform.position, position, Time.deltaTime * speed);
         }
     }    
     
     void NextItem () {
         selectedIndex++;
         
         if (selectedIndex >= obejctList.Count())
             selectedIndex = 0;
     }
     
     void PreviousItem () {
         selectedIndex--;
         
         if (selectedIndex < 0)
             selectedIndex = obejctList.Count() - 1;
     }

            // Get the Item in the current index
           public Transform GetSelectedItem () => obejctList[selectedIndex];
 }


As a final note play around with rather subtracting the angle index offset if its displaying the wrong items.

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 ellisju · Jul 07, 2019 at 07:05 PM

Thank you for the code! I ended up getting it to work a little differently but your stuff got me thinking about it in a different way. I appreciate the help!

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

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

146 People are following this question.

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

Related Questions

How do I get an object to rotate around a different axis based on the raycasts hit angle? 0 Answers

how to set rotation / position of an object on trigger? 2 Answers

Rotate an object to a given point 2 Answers

Restricting GameObject Rotation 1 Answer

Testing if object rotation differs from given rotation and rotating towards it? 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