• 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 tommasoromano · May 12, 2021 at 07:59 PM · rotationvector3direction

How to rotate a direction (create a direction from another direction)

Hi, I want a function that returns a random direction based on only two parameters: position and his normal.

  private Vector3 generateRotation (Vector3 pos, Vector3 N)
     {
     // return R Vector3 (random direction)
     }

The direction R create an angle of 90 degrees with N. The direction R has a random rotation around N. How can I generate R based only on position and his normal?

Simple Example:

input: pos=(0,0,0), N=(0,1,0) | output: R=(Random.Range(-1f,1f), 0f, Random.Range(-1f,1f)).normalized

Note: it must work on any normal. Example: N=(0,0,1) out R=(0,-1,0) or N=(.25,.25,.25) etc... alt text

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
2
Best Answer

Answer by unity_ek98vnTRplGj8Q · May 12, 2021 at 08:21 PM

I'm not sure what you want to do with the position parameter, any vector always starts at the origin. As far as returning a random vector you have lots of options. The way I would go about it would just be to use the Cross Product. If you are not familiar with the cross product, it basically takes two vectors and returns a vector that is perpendicular to both. In your case you would want ANY vector that is perpendicular to your normal vector, so to randomize your cross product you could just randomize the second vector.

 return Vector3.Cross(N, Random.onUnitSphere);

Technically there is the possibility here that the random vector is parallel to your normal vector, in which case the cross product would return the zero vector. To avoid this, we can just generate another random vector.

 Vector3 output = Vector3.zero;
 while(output == Vector3.zero){
     output = Vector3.Cross(N, Random.onUnitSphere);
 }
 return output;
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 tommasoromano · May 13, 2021 at 09:37 PM 0
Share

Thank you! I used: Vector3.Cross(N, Random.insideUnitSphere)

avatar image
1

Answer by andrew-lukasik · May 12, 2021 at 08:13 PM

Adding position to a direction vector will make it a position vector (no longer a direction)

 Vector3 GenerateRotation ( Vector3 normal ) => Vector3.ProjectOnPlane( UnityEngine.Random.onUnitSphere , normal ).normalized;

100 random rotations for normal = new Vector3(1,1,1)

preview

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 tommasoromano · May 13, 2021 at 09:36 PM 0
Share

Thank you! I used: Vector3.Cross(N, Random.insideUnitSphere)

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

179 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 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

Get rotation from a vector direction. 1 Answer

3D nested Turret Prefab Rotation 1 Answer

Rotating a direction Vector3 by Quaternion 2 Answers

How to get offset postition after rotation ? (Spent hours figuring it out) 3 Answers

Dynamically change Vector3.up/right?,How to make a cylinder chase mechanic? 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