# It seem to me to be stupid... how to get perpendicular Vector3 from 2 gameobjects??

Discussion in 'Scripting' started by danielesuppo, Oct 4, 2021.

1. ### danielesuppo

Joined:
Oct 20, 2015
Posts:
231
Hello,
to be honest I've already solved this problem time ago, but I can't remember how I did it (so maybe I'm really getting stupid...).

I'm working on a "mirror" in my scene (yes, the one that you can see in your bathroom), so I'm using a Camera (I've named it "Camera_mirror") with a RenderTexture, placed in the center of the mirror mesh.
Obviously I have to rotate the "Camera_mirror" in Update, so to be always perpendicular to the 1st person Camera (otherwise it would not be a mirror anymore).

Well, maybe I've lost a bit of my brain, but I'm getting lost in maths...

To start, I've created a simple script to draw the perpendicular from A to B in 2D, and I've attached it to the "B" gameObject (that should be my "Camera_mirror")
P.S. please excuse redundance code, it's just to clarify

Code (CSharp):
1. void DrawLine(){
2.
3.         GameObject B = gameObject;
4.
5.         /// Draw line from A to B
6.         Handles.DrawLine(B.transform.position, A.transform.position);
7.
8.         Vector3 dir = A.transform.position - B.transform.position;
9.         Vector3 left = Vector3.Cross(dir, Vector3.up).normalized * 5f;
10.
11.         /// Draw line perpendicular
12.         Handles.DrawLine(B.transform.position, left);
13.         transform.LookAt(left, Vector3.up);
14.     }

Well, it work just if the "B" gameObject is in the center of the world (position 0,0,0)

If "B" is NOT in the center of the world, the perpendicular is wrong

Please can you help me to get back a bit of my brain?
Many thanks!!

2. ### ensiferum888

Joined:
May 11, 2013
Posts:
306
It seems this line right here is the problem:

Handles.DrawLine(B.transform.position, left);

DrawLine takes two positions as arguments but left here is just a direction.

You should do something like

Code (CSharp):
1. left = B.transform.position + left;
Which is now the position of B + the offset vector.

Bunny83 likes this.
3. ### danielesuppo

Joined:
Oct 20, 2015
Posts:
231
Many many thanks ensiferum888,
I was guessing that it was an offset issue

Anyway, I'm actually so confused that I didn't realized that's not the right way for a mirror...

Last edited: Oct 4, 2021
unityunity