This site uses strictly necessary cookies. More Information

X- Home /

# 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.

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).

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;
```

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?

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.

*Hint:* write a function like `VectorToAngle();`

and maybe `AngleToVector();`

Then `C = AngleToVector(VectorToAngle(A)+VectorToAngle(B));`

**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.

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

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.)

@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.

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.

**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);
}
```

### Your answer

### Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.