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 /
  • Help Room /
avatar image
0
Question by PhilippUmschaden · Feb 15, 2018 at 10:59 AM · vector3perpendicular

How to calculate the perpendicular vector to a direction vector in 3D

I know that question has been asked a few times before, but none of the solutions seem to work for me. I have two different points(A,B) in 3D space. Between those to points I calculate the direction vector. I would like to calculate the perpendicular vector to the direction vector. Right now I have something like this:

 normalVector.x = directionVector.x;
 normalVector.y = directionVector.z;
 normalVector.z = -directionVector.y; 


Unfortunately, that does not work. The angle between the direction vector and my supposedly perpendicular vector is always smaller than 90 degrees. Generally speaking the perpendicular vector of a vector(x,y,z) should be (x,z,-y) if I am not mistaken. But that does not seem to work here. Can anyone help me out?

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 Owen-Reynolds · Feb 15, 2018 at 03:15 PM 0
Share

That's the equation from school for rotating something 90 degrees on a flat plane - it's for 2D space. Usually you flip x/y and negate one. The one you found works for any line where x=0. You could try adding "3D" to searches.

But a hacktastic Unity way is to start with an empty gameObject. Use lookAt to aim it. Then look at it's dot-right or dot-up. Those will both be perpendicular to the way it's aimed. In 3D space there isn't "a" perpendicular line - there are an infinite number making a circle, with your line co$$anonymous$$g straight out if it.

1 Reply

· Add your reply
  • Sort: 
avatar image
3

Answer by yummy81 · Feb 15, 2018 at 02:40 PM

The method which is used to calculate the perpendicular vector is Vector3.Cross. It takes two parameters. Both of them are direction vectors. The easiest way to comprehend it is to remember that Unity uses "left-hand rule". When you arrange your fingers as in the picture, the index finger (pointing upwards) is the direction vector which you supply as the first parameter. Thumb is the finger which represents the direction vector supplied as the second parameter. This way, Vector3.Cross returns the third vector (middle finger), which is also direction vector. And, this is your perpendicular vector. So, let me reproduce my workflow. At first, create three empty gameobjects, name them "A", "B" and "C". Then put the script on one of them (I put it on "A"), and assign all of them to "public Transform[] points" in the inspector. Next, set their positions in the inspector as follows: A(0, 0, -1), B(0, 0, 0), C(0, 1, 0). Now, your points (A and B) are connected with the blue line (represented by thumb and this is the second parameter). The red line is your perpendicular vector. And, the green one (from point B to C) represents the direction vector which you need to pass as the first parameter to Vector3.Cross. Here's the picture and the code:

alt text

     // points A, B and C
     public Transform[] points;
     // Lerp Value slider used to set the tail of the perpendicular vector
     [Range(0f, 1.0f)] public float lerpValue = 1.0f;
     // radius of each wire sphere
     public float radius = 0.1f;
     
     private void OnDrawGizmos()
     {
         if (points==null || points.Length < 3) return;
         // I assigned each gameobject position to appropriate point
         Vector3 A = points[0].position;
         Vector3 B = points[1].position;
         Vector3 C = points[2].position;
         
         // This draws wire spheres for debugging purpose
         Gizmos.DrawWireSphere(A, radius);
         Gizmos.DrawWireSphere(B, radius);
         Gizmos.DrawWireSphere(C, radius);
         
         // this is the direction between A and B vectors. Thumb finger. It is the Vector3.Cross second parameter.
         Vector3 thumb = B - A;
         
         // this is the tail of the perpendicular vector. You can manipulate the position of the tail as you wish, using Lerp Value slider in the inspector.
         Vector3 tail = Vector3.Lerp(A, B, lerpValue);
         
         // this is the first parameter of Vector3.Cross. Direction vector. Index finger.
         Vector3 indexFinger = C - B;
         // middleFinger is your perpendicular vector. It's the direction.
         Vector3 middleFinger = Vector3.Cross(indexFinger, thumb);
         
         // This draws blue line from point A to point B. In other words from point A in the direction of point B.
         Gizmos.color = Color.blue;
         Gizmos.DrawRay(A, thumb);
         
         // This draws green line from point B to point C. In other words from point B in the direction of point C.
         Gizmos.color = Color.green;
         Gizmos.DrawRay(B, indexFinger);
         
         // This draws red line from where the tail is in the direction of perpendicular vector. This is your vector.
         Gizmos.color = Color.red;
         Gizmos.DrawRay(tail, middleFinger);
     }    
 
 

picture.png (103.1 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 PhilippUmschaden · Feb 15, 2018 at 06:12 PM 0
Share

Thank you. Now it works.

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

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

Related Questions

Rotation Perpendicular to Line Segment 1 Answer

How to Stack, Shuffle and deal cards 0 Answers

Can't get correct angle between two world positions 1 Answer

How to play animation depending on distance ?? (Video) 0 Answers

Get object's rotation around axis defined by vector 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