Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 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 hsr38 · Oct 29, 2014 at 02:58 AM · rotationvector3quaternionmath

Calculate Quaternion.LookRotation manually

Hi,

After many hours of research and getting in nowhere, I was wonder if anybody here knows how on earth Unity does the Quaternion.LookRotation calculation.

Im asking this because Im working on a school project which I have to create my stuff from scratch (Quaternion class,Vector class etc). So the look operation is the only thing I didn’t managed to work.

I would like to see both ways, meaning the "without the up vector" and "with up vector" if there is any other differences besides the automatic use of Vector3.up in the first one.

Thanks for your time.

Comment
Add comment · Show 2
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 robertbu · Oct 29, 2014 at 03:00 AM 0
Share

Not an exact duplicate, but there is some source in this answer:

http://answers.unity3d.com/questions/467614/what-is-the-source-code-of-quaternionlookrotation.html

avatar image hsr38 · Oct 29, 2014 at 11:08 AM 0
Share

I did see this question before and in fact, it was the one which made me believe that a look rotation would be executed from a quaternion calculation. Before that, I did not even know from where to start. Thanks!

1 Reply

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

Answer by robertbu · Oct 29, 2014 at 03:49 AM

If I had to write Quaternion.LookRotation(), I might do something like this:

 function MyLookRotation(dir : Vector3, up : Vector3) : Quaternion {
     if (dir == Vector3.zero) {
         Debug.Log("Zero direction in MyLookRotation");
         return Quaternion.identity;
     }
     
     if (up != dir) {
         up.Normalize();
         var v =  dir + up * -Vector3.Dot(up, dir);
         var q = Quaternion.FromToRotation(Vector3.forward, v);
         return Quaternion.FromToRotation(v, dir) * q; 
     }
     else {
         return Quaternion.FromToRotation(Vector3.forward, dir);
     }
 }

Note this is built on Quaternion.FromToRotation(), which you would then have to unravel if you need everything from scratch.

http://stackoverflow.com/questions/1171849/finding-quaternion-representing-the-rotation-from-one-vector-to-another

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 hsr38 · Oct 29, 2014 at 11:06 AM 0
Share

$$anonymous$$y goodness I spent so much time trying to find the solution you wrote! I cannot even imagine how you could possibly arrive at this. Anyway thanks alooooot for your answer.

If you have the time, could you explain why you did multiply the dir+up with the negative dot(up,dir) ?

avatar image hsr38 · Oct 29, 2014 at 11:33 AM 0
Share

If a millionaire read his answers, please! Buy him a Ferrari. Don't forget to give him an up vote

avatar image robertbu · Oct 29, 2014 at 03:00 PM 0
Share

I don't know if this is an exact match to Quaternion.LookRotation(), but in a bit of testing, it behaves in a similar fashion. What this code does is use the 'up' vector as a normal on a plane. The 'dir + up..' part take the direction and projects it on the plane defined by the 'up' vector. The result is that the first FromToRotation() (line 10) uses the 'up' vector as an axis of rotation. After that rotation is done...after the rotation has aligned things as much as it can using 'up' as the axis...it rotates from that direction to the final direction in a second rotation. Note that if 'dir == up' then you cannot use it as an axis.

avatar image mikii123 · Jan 10, 2017 at 05:55 PM 0
Share

This is the best solution I have ever found! I'm making my own engine and was trying to replicate the famous LookRotation function. This algorithm works like a charm! I used this code for FromToRotation function.

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

3 People are following this question.

avatar image avatar image avatar image

Related Questions

Rotate object based on hit normal + matching camera direction 0 Answers

Rotate an object so its up vector matches a sphere 1 Answer

Transform a Vector by a Quaternion 1 Answer

LookRotation Vector3 is Zero, Yet Slerp Still Rotates? 2 Answers

Instantiate GameObject towards player 0 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