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
1
Question by rish209 · Jul 26, 2016 at 07:38 AM · rotationquaternioncubesrelative rotation

Modifying the Relative Orientations of Two Cubes

In the project I'm currently working on, I have 2 cubes. Once a button is pressed by the user I want one of the cubes' rotation to be rounded to the nearest 90 degrees on every axis relative to the other cube. Is there any way to do this? Thanks!

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

1 Reply

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

Answer by Bunny83 · Jul 26, 2016 at 11:48 AM

This should do what you want:

 private static Vector3 AxisAlign(Vector3 aVec)
 {
     var size = new Vector3(Mathf.Abs(aVec.x), Mathf.Abs(aVec.y), Mathf.Abs(aVec.z));
     if (size.x > size.y)
     {
         if (size.x > size.z)
             return Vector3.right * Mathf.Sign(aVec.x);
     }
     else if (size.y > size.z)
         return Vector3.up * Mathf.Sign(aVec.y);
     return Vector3.forward * Mathf.Sign(aVec.z);
 }

 public static void AxisAlignTransform(Transform aTrans, Transform aReference)
 {
     Vector3 f = AxisAlign(aReference.InverseTransformDirection(aTrans.forward));
     Vector3 u = AxisAlign(aReference.InverseTransformDirection(aTrans.up));
     f = aReference.TransformDirection(f);
     u = aReference.TransformDirection(u);
     aTrans.rotation = Quaternion.LookRotation(f,u);
 }

Just call AxisAlignTransform and pass your cube you want to align as first parameter and the reference object as second parameter.

The trick here is to simply convert the target cube's forward and up vector into the local space of the reference transform. Now they are local vectors and we just need to find the largest value of the 3 vector components as that's the major direction and will be the closest direction we want.

Once axisaligned we just convert the local vectors back into worldspace where we can use them in LookRotation to calculate the new orientation.

Comment
Add comment · Show 2 · 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 rish209 · Jul 26, 2016 at 05:02 PM 0
Share

Thanks for the help. This seems to work perfectly.

avatar image Bunny83 rish209 · Jul 27, 2016 at 03:12 PM 0
Share

Great since i haven't tested it :D

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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

Set a rotation relative to position between two objects. 0 Answers

How to Rotate Relative To World 1 Answer

Mouse Orbit on a Different XY Plane? 2 Answers

When applying a 90 degree rotation to Euler Angles, it is over/undershooting sometimes.. 2 Answers

Transform Z axis rotation from center of screen toward mouse cursor with Quaternion.LookRotation for a top down game. 2 Answers


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