• 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
Question by chanfort · Oct 14, 2014 at 04:03 PM · vector3quaternionangle

Random perpendicular vector3 after rotation

I am generating random vectors3 (lets name it R), which would be perpendicular to given vector3 (lets name it A). If vector A is defined as:

 Vector3 A = new Vector3(0f,1f,0f);

I can easily get any random vector3 which would be perpendicular to A by:

 Vector3 R = new Vector3(Random.Range(-1f,1f), 0f, Random.Range(-1f,1f));

How I could create random vector R which would be perpendicular to any vector in 3d space?

alt text

vectors.png (43.9 kB)
Comment

People who like this

0 Show 0
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
Best Answer

Answer by robertbu · Oct 14, 2014 at 04:05 PM

Given any two non-parallel vectors, you can generate the perpendicular by Vector3.Cross(). The direction of that vector will depend on the order you pass the two parameters in the Cross() call.

http://docs.unity3d.com/ScriptReference/Vector3.Cross.html

Comment

People who like this

0 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 Scribe · Oct 14, 2014 at 04:39 PM 0
Share

To add to this, if you only have 1 vector (i, j, k), then there are an infinite number of perpendicular vectors, all of which line on the plane of equation ix + jy + kz = 0, you can then solve that equation to find related x, y, z values for a random perpendicular vector, for example:

 Vector3 v = Vector3.zero;
 while(v == Vector3.zero){
     v = new Vector3(Random.Range(0.0f, 1.0f), Random.Range(0.0f, 1.0f), Random.Range(0.0f, 1.0f));
 }
 Vector3 vPerpendicular = Vector3.one;
 if(v.x != 0){
     vPerpendicular.x = -(v.y*vPerpendicular.y + v.z*vPerpendicular.z)/v.x;
 }else{
     vPerpendicular.x = Random.Range(0.0f, 1.0f);
 }
 if(v.y != 0){
     vPerpendicular.y = -(v.x*vPerpendicular.x + v.z*vPerpendicular.z)/v.y;
 }else{
     vPerpendicular.y = Random.Range(0.0f, 1.0f);
 }
 if(v.z != 0){
     vPerpendicular.z = -(v.y*vPerpendicular.y + v.x*vPerpendicular.x)/v.z;
 }else{
     vPerpendicular.z = Random.Range(0.0f, 1.0f);
 }
 Debug.Log(vPerpendicular);

vPerpendicular will be a random vector in the plane for which v was the normal

Hope that helps!

avatar image chanfort · Oct 15, 2014 at 12:40 PM 0
Share

Cross product with any other random vector seems to be working. Thanks.

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

1 Person is following this question.

avatar image

Related Questions

Make a side of an object LookAT another object 1 Answer

Rotate floor plane in-game via C# script 1 Answer

Raycasting at an offset angle? 4 Answers

Angles from quaternion/vector Problem. 2 Answers

Find if a target point is left or right, and infront or behind a character. (Angle between three points?) 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