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 mat · Jun 25, 2011 at 12:29 PM · quaternion

Problem Understanding Quaternions

I'm trying to get my head around what exactly Quaternion.LookRotation does since the documentation is vague.

If I create a zx (horizontal) ground plane and create a stick like game object on it (like a clock hand) I can easily get that stick to face a target pos using:

 Vector3 toTarget = target.transform.position - transform.position;
         
 Quaternion newRot = Quaternion.LookRotation(toTarget, Vector3.up);
         
 transform.rotation = newRot;

This makes sense since I'm reading that function (perhaps incorrectly) as rotating the stick around the up vector to face in the direction of toTarget.

If I create an xy (vertical) plane and position a stick on it I'd expect to be able to rotate that stick by using this:

 Vector3 toTarget = target.transform.position - transform.position;
             
 Quaternion newRot = Quaternion.LookRotation(toTarget, Vector3.forward);
             
 transform.rotation = newRot;

Alas that does all sorts of weird stuff. The stick is rotated along all axis instead of just the the z axis. I can correct it by setting the x and y components of newRot to zero before applying it to the transform but I don't understand why i should have to -- why it should behave differently to the first example.

I've searched around a bit but I cannot find anything that explains what's happening here. Can someone help me?

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 Bunny83 · Jun 25, 2011 at 12:42 PM

LookRotation will always rotate the object into the direction you give. The upvector just specifies where the y-axis will point to. You're not rotating around the upvector, you're rotating around the look-vector. I'm not sure where your target is. if it's on the plane it should work your way. You may invert the Vector3.forward vector if the stick is rotated around the lookvector by 180°. Also it would be better to use a vector from the plane space as up vector. Use .forward / .up / .right of the planes transform.


edit

Where does the local axis of your plane point at? If you use the default plane that comes with Unity the face will look at the local up vector (y-axis)

If you use the plane's upvector as upvector it should work, no matter how you rotate your plane as long as the target is on the plane.

 Transform target; // assign the target object
 Transform plane; // assign the plane
 
 void Update()
 {
     Vector3 toTarget = target.position - transform.position;
     Quaternion newRot = Quaternion.LookRotation(toTarget, plane.up);
     transform.rotation = newRot;
 }
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 Joshua · Jun 25, 2011 at 12:56 PM 0
Share

Right. You have to understand that when an object is rotated to look at something, it's local z-axis will point it it. But it's head can still be rotated 360 degrees around that axis (upside down for instance) so you have to specify what the local y-axis should be.

If an object is already rotated to have his head 'up' (local y-axis alligns with the planes local y-axis) and you give it a target on that plane, then I expect it will rotate the way you expected.

avatar image mat · Jun 25, 2011 at 02:00 PM 0
Share

Hi Guys,

Thanks very much for your quick answers. I'm still really struggling with this though.

The target is on the same plane as the stick.

Bunny83: what is the 'look-vector'? Is that the toTarget value in my example? If it is then I don't understand why you say the object is being rotated about it. In the horizontal plane example the rotation is around the y-axis until the object's facing vector aligns with toTarget.

Can you provide me with the code I would need to use to rotate an object with local y-axis pointing up, about the z-axis to face a target position? (like looking at a clock face on a wall and rotating one of the hands to face a numeral) That might help me see it more clearly.

avatar image Bunny83 · Jun 25, 2011 at 02:18 PM 0
Share

You have to think 3 dimentional. LookRotation will rotate the local z-axis at the given target. That's the look direction. It will always point exactly at the target. The rotation around the local z-axis is subsidiary. It will rotate the object that it's local y-axis (up) will point "in the direction of the given upvector" but not necessarily exactly because the direction to the target is the primary direction. If your given upvector is exactly perpendicular to your look direction it will match the y-axis.

I'll add an example

avatar image Joshua · Jun 25, 2011 at 02:21 PM 0
Share

Yeah I was also struggling to understand what would happen if the two were not perpendicular. Turns out the 'looking' is the important one, and if the 'up' one isn't perpendicular it just picks on that's close.

avatar image mat · Jun 27, 2011 at 09:34 AM 0
Share

I'm starting to see it now.

Bunny: I'd still like to see your example code for the clock example above if you have the time.

Thanks a lot guys.

avatar image
1

Answer by aldonaletto · Jun 25, 2011 at 02:30 PM

When you assign a new rotation to a transform, it replaces the original one. If the object was originally rotated in order to face what you consider its forward direction, then assigning a new rotation will make it loose the original setup, and it will be left looking to the wrong side. If this is the case, you should save the initial rotation in Start, then always multiply this rotation to the new one before assignment:

 Quaternion rot0;
 
 function Start(){
   rot0 = transform.rotation;
 }
 
 // apply the new rotation combined with the original one:
   transform.rotation = rot0 * newRot;

NOTES:
1- Quaternion operations aren't commutative: don't change the order, or you can have weird results.
2- I'm a JS guy, thus forgive me for any eventual C# error.
3- More details about quaternion multiplication at: http://unity3d.com/support/documentation/ScriptReference/Quaternion-operator_multiply.html

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

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

Projectile Rotation To Match Direction Shot? Mobile Joystick 2 Answers

Rotating an Object to its Original Angles after certain distance from ground 1 Answer

Lerp Rotation C# 3 Answers

Rotate object to a specific angle and then stop. 2 Answers

Quaternion from US to C# 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