Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 xandermacleod · Jul 05, 2013 at 10:41 AM · vector3vector2

Need help with Vector3s and Vector2s

Hi there,

(I have edited this post to make it more understandable).

In the following diagram, I have 2 Vectors - Vector A and Vector B. I want to find Vector C.

alt text

Is there a way to do this? / Is there a way to specific the vector using Transform Direction rather than forcing VectorA to be the transform of some object?

(P.S. Im using C#, and whilst Vector A and C will be Vector3s, VectorB will be a vector2). (Currently in my game VectorA represents a special unique Vector to do with my scene camera, VectorB represents a player's input using the analogue sticks, and VectorC is supposed to represent a direction for that player's character to be moving).

vectorissue.jpg (132.0 kB)
Comment
Add comment · Show 4
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 Philipp · Jul 05, 2013 at 11:37 AM 0
Share

I'm not a 100% sure I understand what you want, but if I read you correctly, Transform.TransformDirection() should help you do what you want: link

Suppose you have a Vector3 "dir" that specifies the direction you want to go, relative to the camera (so (0,0,1) would be forward, (-1,0,0) would be left, etc.):

 Vector3 tempVector;
 Vector3 relativeDir = cameraObject.transform.TransformDirection(dir);
 tempVector = new Vector3(relativeDir.x, 0, relativeDir.z).normalized;
 return tempVector;

avatar image xandermacleod · Jul 05, 2013 at 12:56 PM 0
Share

I have edited the above posting to better represent what I mean. Could you let me know if TransformDirection can work for independent Vector3s ins$$anonymous$$d of needing to specify a vector3 from some gameObject's transform?

avatar image Jessy · Jul 05, 2013 at 02:24 PM 0
Share

TransformDirection is just matrix rotation, using the XYZ of the transform as the vector basis. This will be impossible to solve without expressing A as one of the three axes. You can then find the other vector you need, for 2D rotation, using the cross product of A and the axis that B doesn't contain. But you shouldn't bother, if you can just get the vectors you need from a Transform.

avatar image private void · Jul 05, 2013 at 02:44 PM 0
Share

Hint: write a function like VectorToAngle(); and maybe AngleToVector(); Then C = AngleToVector(VectorToAngle(A)+VectorToAngle(B));

2 Replies

· Add your reply
  • Sort: 
avatar image
2
Best Answer

Answer by robertbu · Jul 05, 2013 at 02:47 PM

Using Vector3.up as the reference vector for the angle, we can do this:

 Quaternion q = Quaternion.FromToRotation(Vector3.up, v3B);
 Vector3 v3C = q * v3A;

Where v3A, v3B, and v3C are the vectors A, B, and C from your diagram.

Comment
Add comment · Show 4 · 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 Jessy · Jul 05, 2013 at 03:43 PM 0
Share

Quaternions are overkill on the necessary complexity front, but it works!

avatar image Owen-Reynolds · Jul 05, 2013 at 06:35 PM 0
Share

It looks a little like the poster wants to reflect A around B (as if B were a normal and A/C was a ball bouncing.) which would be abut the same math, but

q=Quaternion.FromToRotation(vecA, vecB); vecC=q*vecB;

But the center bottom picture clearly shows that V3.up is the reference, and they want A->C to be same same as up->B (why would you want that? But the lower 2 right pics are pretty clear.)

avatar image robertbu · Jul 05, 2013 at 06:52 PM 0
Share

@Owen Reynolds - I too had trouble first understanding what the OP wanted, but reading the full question, I'm pretty sure I'm giving him what he asked for. I'm guessing he has an object at some arbitrary rotation in 3D space and he wants to rotate it around the world 'z' axis through the object using the joystick to calculate an absolute rotation (i.e. compass rose). Given a fuller understanding of the probem, there might be easier better ways to accomplish this task.

avatar image xandermacleod · Jul 05, 2013 at 07:33 PM 0
Share

sorry if I didnt make it very clear. I had a bit of a hard time explaining myself properly. Like 'private void' suggested, I wanted some kind of way of adding together the angle of VectorA and VectorB to get the angle of VectorC and then to get a normalized vector using that angle. It wasn't so much a 'mirroring' kind of job or anything like that. Indeed the reference I was intending to use was Vector3.Up I probably shouldve used a 3d diagram rather than a 2d one... :S

Anyway, I have yet to check out the quaternion solution but it sounds to me like it should work. I'll update the thread when I can test it all out.

avatar image
0

Answer by Jessy · Jul 05, 2013 at 07:57 PM

Transform.TransformDirection is equivalent to Transform.localToWorldMatrix.MultiplyVector.

You need to decide what axes B is expressed in. Without having your axes labeled, I'm going with the assumption that your vertical axis is Y and your horizontal is X.

B will be defined in a space where A is Y. The X axis is trivially found in 2D. Then you create a 2x2 matrix with these vectors as the columns, and you can transform local vectors to world ones. Matrix4x4.MultiplyVector is overkill, because it handles up to 3 dimensions of rotation; if it matters for performance, you can make your own Matrix2x2 struct.

 [SerializeField] Vector3 a;
 [SerializeField] Vector2 b;
 
 void OnDrawGizmos() {
     Vector2 ay = ( (Vector2)a ).normalized;
     Gizmos.color = Color.green;
     Gizmos.DrawRay(Vector3.zero, ay);
 
     Vector2 ax = new Vector2(ay.y, -ay.x);
     Gizmos.color = Color.red;
     Gizmos.DrawRay(Vector3.zero, ax);
 
     Matrix4x4 aToWorld = new Matrix4x4();
     aToWorld.SetColumn(0, ax);
     aToWorld.SetColumn(1, ay);
     Vector3 b_world = aToWorld.MultiplyVector(b);
     b_world.z = a.z;    // I don't know if this is what you want.
     Gizmos.color = Color.cyan;
     Gizmos.DrawRay(Vector3.zero, b_world);
 }
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

20 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

Related Questions

C# Convert Vector3[] to Vector2[] 3 Answers

Create custom constructors for certain Value types/Objects (Vector3, Transform, etc...) 2 Answers

[HELP] Translating vector 2 co-ordinates to real space 1 Answer

What happens when I populate a ray with a vector2? 1 Answer

Transfer values from C# to Javascript 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