• 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
5
Question by lachesis · Sep 11, 2013 at 04:10 PM · transform.positionlookrotation

Difficulty understanding LookRotation

While I understand that the Quaternion.LookRotation function generates a quaternion from a specified target's transform.position, I've often seen the difference between the target position and the gameObject's position used to rotate the gameObject.

E.g.

 Quaternion.LookRotation(target.position - transform.position)

rather than the expected

 Quaternion.LookRotation(target.position)

I'm having difficulty understanding why this is the case (or why it works at all).

Comment
Add comment · Show 1
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 Exalia · Sep 11, 2013 at 04:13 PM 0
Share

I am also curious as to why this is haha, I've always used the first example but never known why

1 Reply

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

Answer by Jamora · Sep 11, 2013 at 04:28 PM

The LookRotation method takes a direction as its parameter. A direction can be considered the straight path from 0,0 to the vector's end position. It's also known as the relative position. So on a 2D plane, a direction vector of (2,1) will, add 2 to x and 1 to y.

The starting position does not need to be (0,0), in fact, the direction from (3,0) to its target (5,1) is still (2,1), that is endPoint - startPoint.

So, in short, if you use Quaternion.LookRotation(target.position) you are computing the direction from the world origo (0,0,0) to target.position, whereas using Quaternion.LookRotation(target.position - transform.position) computes the direction from transform.position to target.position.

EDIT:

My knowledge of Quaternions is limited to knowing that they can be considered a 4D gimbal. You know, with 4 of those moving circles instead of the three shown on the wikipedia page. That's been enough to peacefully co-exist with them... though I have been using euler angles.

I'll try to explain the concept of direction more thoroughly, then. Consider vectors A and B in a 2D plane:

alt text

Vectors can be considered to be many things. To name a few examples: direction, length and position.

A direction can be defined as a way to get from somewhere to somewhere else. In our illustration, Vector A is a direction but Vector B is not a direction. Why so? An example will illustrate the situation.

Let's say we are standing at (3,1) and want to go to the direction Vector A points to. So, we take two steps right, and one up. If you now draw the path we took, you would find that the path is indeed parallel to Vector2. If we tried the same with Vector B - taking half a step left, then two up - the two lines would not be parallel. This is because Vector B does not start from the origin, (0,0). Now we can easily draw the conclusion: All directions must have their starting point at the origin: (0,0).

How can we then find what direction our Vector B has? The answer is simple: we need to move the vector so it begins from (0,0). In our case, Vector B needs to be moved half a step right, and half a step down, so we subtract the start point from the end point. Our new direction vector is (-0.5 -(-0.5), 2 - 0.5) = (0, 1.5). If we now move from any starting point using that direction, we will always move straight up, and the lines are again parallel.

Normally directions are given in their normalized form. That is, all values are within the inclusive (0,1) range. This is because there always exists a scalar that gives any parallel direction of a different length. The way this normalized direction can be computed, is using a unit circle (a circle with a radius of 1), and computing the intersection point of the direction vector with the unit circle. The intersection point of any direction and a unit circle is the normalized direction vector. If there is no intersection, then the direction vector may be multiplied by an appropriate scalar. Computing the intersection point is basic trigonometry. My calculator says the normalized direction of Vector A is (0.89, 0.44). The scalar to get Vector A form this normalized direction is ~2,247.


vectorillustration.png (16.7 kB)
Comment
Add comment · Show 1 · 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 lachesis · Sep 11, 2013 at 06:11 PM 0
Share

Thanks, Jamora. That clarifies why one would include the transform.position of the source object.

I'm still confused about the concept of "direction" and how a Quaternion is calculated from position variables. It looks as if my basic working concept of a Quaternion is flawed. It appears that it's a topic that requires some research to understand.

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

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

17 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

Related Questions

Quaternion.LookRotation 2 Answers

Rotate multiple objects around a object, maintaining their own trajectory(not rotating their local forward vector) 1 Answer

Camera following/looking at aircraft 1 Answer

Quaternion to look at Recast hit-point (On One Axis) 0 Answers

How to stop LookRotation() spinning problem? 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