• 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 Christophera1999 · Apr 16, 2018 at 02:15 AM · rotationmultiple objectsalignmentrotatetowards

How can I obtain the overall rotation of multiple selected gameobjects in the unity editor?

I am trying to align a torus to the rotation of a hexagon created by 6 separate tubes. Is there a way to do this in the editor? Any help would be greatly appreciated!alt text

torus.jpg (481.1 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
2

Answer by MarioSantoso · Apr 16, 2018 at 03:33 AM

Group the tubes (place the tubes as children under another gameobject). Then you can have the rotation of the hexagon.

Comment
Add comment · Show 5 · 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 Christophera1999 · Apr 16, 2018 at 12:49 PM 0
Share

@$$anonymous$$ilsnus Thank you, but when I try to do that, though the hexagon has a rotation of zero, if I set the torus rotation to zero, they are still not aligned. I am trying to get the torus to encompass the hexagon, while at the same time sharing its rotation. Is there a way to go about doing that within the unity editor? Thanks!

avatar image MarioSantoso Christophera1999 · Apr 16, 2018 at 04:21 PM 0
Share

When you are building the hexagon with tubes, build them parallel to an axis. From your image, I think you already rotated the tubes before grouping them, thus why the rotation is already off.

Don't rotate the tubes before grouping them.

avatar image Christophera1999 MarioSantoso · Apr 19, 2018 at 12:14 AM 0
Share

@$$anonymous$$ilsnus Thank you, but I am unable to do that because the tubes are part of a larger tubular structure, where each tube has precalculated rotation. Is there another way to accomplish this?

Show more comments
avatar image
0

Answer by Bunny83 · Apr 19, 2018 at 02:37 AM

I'm not that familiar with that molecule ^^ however are you actually sure that this ring is actually "flat" (all bonds lie in the same plane)? If that's the case just take two bonds which are not parallel and calculate the cross product between the two direction vectors. This gives you the normal of your ring. If the bonds are not in the same plane but just close to a certain plane you can calculate the cross product between each neighboring bond pair which gives you 6 normals. Just take the arithmetic mean (add them up and renormalize) to get the average normal.


Once you have the normal you can use Quaternion.LookRotation(normal) as rotation for your torus.


If you need more help you should provide more information on how you actually place those bonds and maybe show how each bond object is rotated (which axis of the bond object points along the bond?).

Comment
Add comment · Show 4 · 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 Christophera1999 · Apr 19, 2018 at 02:50 AM 0
Share

Thanks! How exactly would I do a cross product? The bonds and bond angles are calculated by putting the molecule in its lowest energy conformer. It is then imported into unity as an FBX, so the bonds are rotated automatically.

avatar image Bunny83 Christophera1999 · Apr 19, 2018 at 05:28 AM 0
Share

That means your ring is one single mesh? In your description you said there are 6 seperate tubes...

Well in the case you only have a single model you either have to use the same math used to calculate those angles or access the vertices of the mesh manually. However analysing the vertices can be tricly because if it's a single mesh you can not simply seperate the vertices for the 6 tubes.


Just to be more exact now, the following assumes your mesh only contains those 6 tubes. No other geometry must be part of the same mesh:

  • Since you imported the mesh as FBX you have to make sure you mark it as "readable" in the importer.

  • Depending on how the mesh got imported there will be either a $$anonymous$$eshFilter or a Skinned$$anonymous$$eshRenderer which has the mesh reference. So use GetComponent to get access to the right component and access the $$anonymous$$esh through it's shared$$anonymous$$esh property

  • Get the vertices array into a local array.

  • Find the local space center point of the mesh by calulating the arithmetic mean of all vertices. If your ring is actually centered around local 0,0,0 you can of course skip this step.

  • Create relative vectors from the center to each vertex. To be on the safe side we just calculate all possible combinations but ignore vertices which are too close together.

  • Once we have all normals we have to make sure they point in the same principal direction. A simple dot product helps to decide if we should flip the normal or not.

  • Finally just combine all the normals to get the average.

Something like this:

 var mf = GetComponent<$$anonymous$$eshFilter>();
 var mesh = mf.shared$$anonymous$$esh;
 var verts = mesh.vertices;
 // calculate center
 Vector3 center = verts[0];
 for(int i = 1; i < verts.Length; i++)
     center += verts[i];
 center /= verts.Length;
 
 //get all normals
 List<Vector3> normals = new List<Vector3>();
 for(int i = 0; i < verts.Length-1; i++)
 {
     var dir = (verts[i] - center).normalized;
     for(int n = i+1; n < verts.Length; n++)
     {
         var dir2 = (verts[n] - center).normalized;
         if ($$anonymous$$athf.Abs(Vector3.Dot(dir, dir2)) < 0.88f)
         {
             normals .Add(Vector3.Cross(dir, dir2));
         }
     }
 }
 
 // unify  and add up the normals
 Vector3 refNormal = normals[i];
 for(int i = 1; i < normals.Count; i++)
 {
     if (Vector3.Dot(refNormal, normals[i]) < 0)
         refNormal -= normal[i];
     else
         refNormal += normal[i];
 }
 refNormal.Normalize();
 var rotation = Quaternion.LookRotation(refNormal);

avatar image Bunny83 Bunny83 · Apr 19, 2018 at 05:30 AM 0
Share

Note i've written this from scratch here on UnityAnswer without any syntax checks. So it may compile but it's meant as sort of pseudo code as example.

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.

Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

110 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

Related Questions

Enemy Rotation AI 0 Answers

How to rotate object slowly on only Z axis 2 Answers

Child transforms screwed up by parent's rotations 1 Answer

How to find the closest 4 verts/corners ( along with their orientation ) of a rectangular object in relationship with another object? 1 Answer

How to rotate towards the mouse on one axis while snapping 0 Answers

  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges