Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
13 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
1
Question by arjenvklaveren · Nov 19, 2021 at 08:11 PM · vector3mathtrigonometry

How to transform/rotate a vector onto the same plane as another vector

I have 2 vectors, a predetermined vector and a vector with random x and z values. The random vector starts with a y value of 0, but I need to set it to a value that is in the same plane as the predetermined vector.

For example, I have the predetermined vector (3, 1, 2) and a vector with values (2,0-2). How can I make sure the effect in the below image is achieved? These are non-normalized vectors, in my game the vectors are normalized. alt text

fromtovector.png (151.9 kB)
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
1
Best Answer

Answer by andrew-lukasik · Nov 20, 2021 at 12:05 AM

link text

 using UnityEngine;
 public class AdventuresInVectorProjections : MonoBehaviour
 {
     [SerializeField] Vector3 _tangent = Vector3.one;// this is your "predetermined vector"
     [SerializeField] Vector3 _approxNormal = Vector3.up;// an additional normal to help define a plane
     void OnDrawGizmos ()
     {
         Vector3 betterNormal = Vector3.Cross( _tangent.normalized , -Vector3.Cross(_tangent.normalized,_approxNormal) );
         Vector3 X0Z = Quaternion.Euler(0,((System.DateTime.Now.Millisecond % 1000)/1000f)*360f,0) * Vector3.right;
         
         // option 1, point above X0Z that lies on a plane:
         new Plane{ normal=betterNormal }.Raycast( new Ray{ origin=X0Z , direction=Vector3.up } , out float Y );
         Vector3 XYZ = new Vector3( X0Z.x , Y , X0Z.z );
 
         // option 2, nearest point on a plane:
         // Vector3 XYZ = Vector3.ProjectOnPlane( vector:X0Z , planeNormal:betterNormal );
 
         Vector3 pos = transform.position;
         Gizmos.color = Color.magenta; Gizmos.DrawLine( pos , pos + _tangent );
         Gizmos.color = Color.cyan * 0.5f; Gizmos.DrawLine( pos , pos + _approxNormal );
         Gizmos.color = Color.cyan; Gizmos.DrawLine( pos , pos + betterNormal );
         Gizmos.color = Color.green * 0.5f; Gizmos.DrawLine( pos , pos + X0Z );
         Gizmos.color = Color.green; Gizmos.DrawLine( pos , pos + XYZ );
         Gizmos.matrix = Matrix4x4.Rotate( Quaternion.LookRotation(_tangent,betterNormal) );
         Gizmos.color = new Color( 0.1f , 1f , 0.1f , 0.1f );
         Gizmos.DrawCube( pos , new Vector3{x=3,z=3} );
     }
 }
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
avatar image
2

Answer by benjmitch · Nov 19, 2021 at 10:32 PM

Part 1

The problem needs a bit more specification - "in the same plane as the predetermined vector" doesn't define a unique plane. For instance, the x=3 plane passes through it, as do the y=1 and z=2 planes. Generally, you need three points to define a plane, or a normal to the plane and one point (usually, a point on that normal vector), or some other definition with enough constraints in it.

Taking the diagram as indicative, though, what it /looks/ like is that the plane shown is defined as including also the y-axis (assuming y-axis is marked green, as in Unity). In which case, it's the plane 2x=3z, which can also be defined as having the normal vector [2, 0, -3] (errr... I think...) and passing through the origin. We could define the plane shown with these two data:

Normal vector N = [2, 0, -3] Point in plane P = [0, 0, 0]

Doesn't matter if that's actually your plane, the point is you need it in some standard form.

Part 2

The problem needs a bit more specification, though, again - "in the same plane as the predetermined vector" doesn't define a unique point in a given plane. The diagram seems to be showing projection along the blue axis (z?), but the vector (2, ?, -2) suggests it's a projection in y. So you'll need to specify more about the result than just it being in the plane.

A common extra bit of information is to say 'the nearest point on the plane', so I'll go with that, but a projection along any axis, or indeed any direction, is also a thing you might want to do. The procedure for those is a bit different, but the tools are from the same toolbox.

Part 3

Assuming you have a normal vector, N, and any point in the plane P, you can project your 'random vector' X onto the nearest point on that plane by doing:

X += Vector3.Project(P, N) - Vector3.Project(X, N)

noting that you can precompute the first term since it's the same for any X.

The fact that this is so neat underlines why people like to represent planes in N/P format.

Forgive any silly mistakes, I'm not testing this.

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 benjmitch · Nov 20, 2021 at 09:22 PM 0
Share

I learned some things from the answer by andrew-lukasik. Unity, of course, has a Plane() class that is an alternative way of perfor$$anonymous$$g the projection in Part 3. Plane(N, P).ClosestPointOnPlane(X) would be the foo, if I'm reading the docs right. Does exactly the same maths as shown above, I suspect, but its meaning is clearer. And, as andrew-lukasik shows, you can use the Raycast() method of Plane() to solve for the other points mentioned in Part 2.

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

171 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

Related Questions

How to get a vector3 (postion) 1 unit away from another in the direction of a 3rd vector3? 2 Answers

Given a vector, how do i generate a random perpendicular vector? 5 Answers

Make something move away from click point. 2 Answers

How to properly offset a Handles.DrawSolidArc by an angle in different axis? 2 Answers

Constant total time over multiple Lerp functions 4 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