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
1
Question by sambsp · Apr 25, 2012 at 11:41 AM · car tutorial

Strange angle computing from car tutorial

Get the car tutorial from Unity tutorial, and when read the C# source in file CarController.cs, I find this line of code:

float angle = -Mathf.Asin(Mathf.Clamp( Vector3.Cross(veloDir, carDir).y, -1, 1));

I don't understand what's angle is, and if you can explain, please also comment why only use y axis. thanks.

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

1 Reply

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

Answer by aldonaletto · Apr 25, 2012 at 12:31 PM

This is a smart way to get the angle between two horizontal vectors - Vector3.Angle is easier but only returns the absolute value, thus you can't say if it's to the right or to the left.
The cross product A x B returns a vector perpendicular to A and B, and with magnitude proportional to the sine of the angle between them. If A and B are unit vectors (with length = 1), the length of the vector returned is Abs(Sin(angle)).
In the case above, carDir and veloDir are probably horizontal vectors, thus the resulting cross product will be a vertical vector - and its Y value is numerically equal to the sine of the angle between them.
For more information about cross product, read the Mother of All Knowledge (aka Wikipedia): http://en.wikipedia.org/wiki/Cross_product

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 sambsp · Apr 25, 2012 at 01:08 PM 0
Share

Aldo,

Very clear. One more question after your comment, if these 2 vectors aren't horizontal, is this algorithm reasonable? If it is, what is the result? Thanks.

avatar image aldonaletto · Apr 25, 2012 at 04:00 PM 0
Share

I think (not 100% sure) that the cross.Y value is the sine of the angle measured between the two vectors in the plane perpendicular to Y. If the two vectors form a plane inclined relative to the world axes, I believe the values returned in X, Y and Z are the sines of the angles projected in their respective perpendicular planes - X is relative to the angle projected in YZ, Y relative to XZ, Z relative to XY.

avatar image fafase · Apr 25, 2012 at 04:20 PM 0
Share

Ok from what I know: you could think the 2 vectors form a triangle (if you join the end of each), then you have a plane. The vector from the cross product will always be perpendicular to that plane. Whatever orientation of the 2 original vectors. I guess that is pretty much what @AldoNaletto was saying.

avatar image aldonaletto · Apr 25, 2012 at 08:41 PM 1
Share

Yes, like the image I borrowed from the wikipedia shows:

.
a and b are the two vectors, theta is the angle and n is the cross product. When a and b are unit vectors, the XYZ components of n are the sines of the angles projected on the corresponding perpendicular planes. If a and b form a horizontal plane (parallel to XZ plane), then n.Y is the sine of theta.

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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

car tutorial problem 0 Answers

How to put Car Tutorial in Assets 1 Answer

Wheels can't touch the ground 1 Answer

Having trouble importing. 0 Answers

about the car tutorial sound problem 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