Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
2 captures
12 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
4
Question by tonycoculuzzi · Jun 23, 2011 at 12:06 AM · vector3hownormalcalculatetangent

How do you find the tangent from a given normal?

Normals in Unity are Vector3s, Meaning they represent a point in space. To my understanding, the way normals are given in unity is basically a Vector3 in relation to (0,0,0). For example: (0.5, 0.2, 0) from (0,0,0) How would you go about calculating the tangent from a normal, or any Vector3 (with normal bounds) in this case?

Comment
Add comment · Show 5
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 Chris D · Jun 23, 2011 at 12:14 AM 0
Share

You're talking about a plane that's normal to your vector, right? Because, technically, there are an infinite number lines that are 90 degrees from any given vector.

avatar image Dreamblur · Jun 23, 2011 at 12:20 AM 0
Share

Actually, the normal that Unity returns is a normalized direction vector and not a point. I know that doesn't answer your question, but a better insight into how things work never hurt anyone either.

avatar image Eric5h5 · Jun 23, 2011 at 12:22 AM 0
Share

A Vector3 doesn't necessarily represent a point in space. With regards to normals, the Vector3 represents a direction. (0, 0, 1) means "1 unit along the z axis".

avatar image aldonaletto · Jun 23, 2011 at 01:02 AM 0
Share

Unity uses Vector3 to store both, points and vectors, what sometimes is very confusing. Vectors are universally (not only in Unity) represented as a point relative to 0,0,0 , as @Tonzie suspected.

avatar image aldonaletto · Jun 23, 2011 at 01:07 AM 0
Share

For what do you want to know the tangent to a normal? In 2D space, a tangent is a line perpendicular to the normal at a given point of a curve. In 3D, however, its equivalent is a plane, as @ChrisD said.

2 Replies

· Add your reply
  • Sort: 
avatar image
9

Answer by Oliver Eberlei · Jun 23, 2011 at 01:45 AM

To get a tangent from a normal (out of the possible infinite number of tangents) you can use the cross product with a union vector

 Vector3 normal = new Vector3( 1, 1, 0 );
 Vector3 tangent;

 Vector3 t1 = Vector3.Cross( normal, Vector3.forward );
 Vector3 t2 = Vector3.Cross( normal, Vector3.up );

 if( t1.magnitude > t2.magnitude )
 {
     tangent = t1;
 }
 else
 {
     tangent = t2;
 }

You need to create two cross products in case the normal is pointing in the same direction as one of the test vectors.

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 Oliver Eberlei · Jun 23, 2011 at 01:04 PM 1
Share

A shorter alternative and probably better one, since it only calculates the second cross product when needed (though I have not checked if it produces correct results)

 Vector3 tangent = Vector3.Cross( normal, Vector3.forward );

 if( tangent.magnitude == 0 )
 {
     tangent = Vector3.Cross( normal, Vector3.up );
 }
avatar image neoUFO Oliver Eberlei · Dec 24, 2019 at 01:06 PM 0
Share

This second solution produces incorrect results, ins$$anonymous$$d your first one works fine.

avatar image
0

Answer by robm123 · Jul 25, 2020 at 11:13 PM

If you want a random tangent you can try the following ie. one that is tangent but in a random orientation to the normal: private Vector3 CreateRandomTangentVector(Vector3 normal) {

     //Creates a vector tangental to the one passed to this function.  Note that the angle of the tangent is random.
     float x = UnityEngine.Random.Range(-100, 100);
     float y = UnityEngine.Random.Range(-100, 100);
     float z = UnityEngine.Random.Range(-100, 100);
     Vector3 randomVector = new Vector3(x, y, z);
     randomVector = randomVector / randomVector.magnitude;

     Vector3 t1 = Vector3.Cross(normal, randomVector);
     Vector3 t2 = Vector3.Cross(normal, Vector3.up); 
     Vector3 tangent; //= new Vector3(0, 0, 0);
     if (t1.magnitude > t2.magnitude)
     {
         tangent = t1;
     }
     else
     {
         tangent = t2;
     }

     return tangent;


 }
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

9 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Orient a vehicle to the ground and in the direction of its travel. 2 Answers

Collision.contacts? 1 Answer

" Binary Save" Stuff - Workaround for Vector3 Serialization 3 Answers

How do you calculate the rotation of a hexagon based on the six Vector3 points 0 Answers

Calculating Velocity 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