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
0
Question by NoSoup4you · Aug 21, 2014 at 10:51 PM · vector3quaternionrotatemove

Getting stumped by vectors and quaternions

I think I had a tenuous grasp on moving/rotating stuff at one point, but after doing a bunch of GUI work and coming back to it I don't understand a thing. For instance, I want a 2d sprite to rotate to face the cursor, and the code that usually gets mentioned is something like this:

 Vector3 direction = targetPoint - transform.position;
 transform.eulerAngles = new Vector3(0, 0, Mathf.Atan2(direction.y, direction.x) * Mathf.Rad2Deg);

First of all, I basically understand why subtracting a vector from another gives you a direction, since they're points in space, but I have a hard time picturing how it works when magnitude is involved. Also, I was always confused at how to tell which one to subtract from which.

The math in the eulerAngles assignment is just gibberish to me.

How does a Unity programmer typically get comfortable with this stuff? Is there a book with a good in-depth chapter on it, and not just "paste this code into your game?"

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
0

Answer by Kiwasi · Aug 21, 2014 at 11:18 PM

Any good high school physics text will have a chapter on vector math. Most game programming books will also have a similar chapter.

Vectors can be visualised as a direction from the origin to the point in space. Adding vectors is simply a case of putting the lines end to end and seeing where it turns up. Subtracting vectors is similar, except the negative vector runs in the opposite direction.

Subtraction is always done as final - initial. So if I have points A and B, and I want to get from A to B, I do B - A.

There are some pictures on the Wikipedia page

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 NoSoup4you · Aug 22, 2014 at 01:21 AM 0
Share

You mean origin as in Unity's world origin at 0,0,0? That could be the source of confusion... I was thinking of vectors as the starting points of something rather than the end.

avatar image Kiwasi · Aug 22, 2014 at 03:16 AM 0
Share

Vectors have several ways to be interpreted. Lets look at (1,2)

This vector can represent a point that is 1 unit across and 2 units up from the origin (0,0)

This vector can also represent the direction from the origin (0,0) to the point described above.

In either case the vector has a magnitude. The magnitude is the straight line distance from (0,0) to (1,2).

avatar image
0

Answer by robertbu · Aug 21, 2014 at 11:20 PM

  • 'magnitude' is just the length of the vector. For angle calculations we typically don't care. We only care about the direction.

  • When doing a subtraction of vectors like A - B, the resulting vector points in the direction of A, so you pick the order depending on which way you want a vector to point.

  • Mathf.Atan2(y, x) * Mathf.Rad2Deg is used a lot in the 2D calculations. Using the 'x' and 'y' of a vector, it tell the angle of that vector from Vector3.right (going in a counter-clockwide direction). I believe the actual values go from -180 to 180. Note the order of the parameters. It is 'y' first then 'x'. I personally tend to give answers using Atan2() for 2D questions because some uses of Quaternions tend to flip the object. That is, the objects points in the correct direction, but it is also rotated 180 degrees.

You can get vector basics from a chapter or two on vectors from a math textbook, and I sure there are extensive web pages on vectors on the web. Almost anything you read in a textbook will be usable with Unity.

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

23 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 avatar image avatar image avatar image avatar image avatar image

Related Questions

Rotating wrong 0 Answers

rotate player on y axis or Vector.up regradless of orientation 0 Answers

Rotate vector around vector? 2 Answers

Rotating a character's velocity? 3 Answers

Rotate Towards doesnt work as expected 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