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
2
Question by vladibo · Jun 02, 2013 at 07:46 PM · quaternionquaternion.lookrotation

What is the source code of Quaternion.LookRotation?

Is there some reference where I can see the native source code? Or similar implementation elsewhere?

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
5
Best Answer

Answer by vladibo · Jun 03, 2013 at 11:17 AM

This is what I was able to come out with. Seems to work. Just note that forward and up vector must be orthogonal, the function doesn't have orthogonization of up vector.

 private static Quaternion QuaternionLookRotation(Vector3 forward, Vector3 up)
 {
     forward.Normalize();
 
     Vector3 vector = Vector3.Normalize(forward);
     Vector3 vector2 = Vector3.Normalize(Vector3.Cross(up, vector));
     Vector3 vector3 = Vector3.Cross(vector, vector2);
     var m00 = vector2.x;
     var m01 = vector2.y;
     var m02 = vector2.z;
     var m10 = vector3.x;
     var m11 = vector3.y;
     var m12 = vector3.z;
     var m20 = vector.x;
     var m21 = vector.y;
     var m22 = vector.z;
 
 
     float num8 = (m00 + m11) + m22;
     var quaternion = new Quaternion();
     if (num8 > 0f)
     {
         var num = (float)Math.Sqrt(num8 + 1f);
         quaternion.w = num * 0.5f;
         num = 0.5f / num;
         quaternion.x = (m12 - m21) * num;
         quaternion.y = (m20 - m02) * num;
         quaternion.z = (m01 - m10) * num;
         return quaternion;
     }
     if ((m00 >= m11) && (m00 >= m22))
     {
         var num7 = (float)Math.Sqrt(((1f + m00) - m11) - m22);
         var num4 = 0.5f / num7;
         quaternion.x = 0.5f * num7;
         quaternion.y = (m01 + m10) * num4;
         quaternion.z = (m02 + m20) * num4;
         quaternion.w = (m12 - m21) * num4;
         return quaternion;
     }
     if (m11 > m22)
     {
         var num6 = (float)Math.Sqrt(((1f + m11) - m00) - m22);
         var num3 = 0.5f / num6;
         quaternion.x = (m10+ m01) * num3;
         quaternion.y = 0.5f * num6;
         quaternion.z = (m21 + m12) * num3;
         quaternion.w = (m20 - m02) * num3;
         return quaternion; 
     }
     var num5 = (float)Math.Sqrt(((1f + m22) - m00) - m11);
     var num2 = 0.5f / num5;
     quaternion.x = (m20 + m02) * num2;
     quaternion.y = (m21 + m12) * num2;
     quaternion.z = 0.5f * num5;
     quaternion.w = (m01 - m10) * num2;
     return quaternion;
 }
Comment
Add comment · Show 3 · 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 aeroson · Sep 19, 2015 at 09:56 AM 1
Share

It seems to me that vector3 is orthogonalized up vector.

avatar image gkillz · Jul 19, 2018 at 08:27 AM 0
Share

is this the actual implementation in unity or u have come up with this ?

avatar image Bunny83 gkillz · Jul 19, 2018 at 01:41 PM 0
Share

We don't know the exact implementation of Unity's LookRotation method as it's defined in native code.


I have not confirmed the code here is right, but it looks like he's using pretty much the same approach mentioned here at the end.

avatar image
1

Answer by Graham-Dunnett · Jun 02, 2013 at 08:35 PM

Dunno what the source code is exactly, but normalize the vectors, take a cross product, then you have 3 vectors that will form a rotation matrix. (So, (0,0,1) multiplied by M gives forward, (0,1,0) multiplied by M gives upward, and (1,0,0) multiplied by M gives right.) Now you have a rotation matrix, so google and get:

http://renderfeather.googlecode.com/hg-history/034a1900d6e8b6c92440382658d2b01fc732c5de/Doc/optimized%20Matrix%20quaternion%20conversion.pdf

which tells you how to get from a matrix to a quaternion.

(You can, of course, license the source code to Unity if you really need to know exactly what's happening under the hood.)

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

18 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

Related Questions

Quaternion Slerp 180 Away from target 1 Answer

How can I instantiate a gameobject facing another gameobject 2D? 0 Answers

"Quaternion.LookRotation" not working when Instantiate 1 Answer

Create an offset to adjust Quaternion rotation 1 Answer

Use curve effect on rotation? 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