0

I have two vector(Vector3) positions of a line on a plane whose normal vector is known .What I am trying to do is draw a line perpendicular to this line on the same plane. Suppose end1 and end2 are two vectors representing the ends of the original line What I am trying right now :

                    var line1 = end1 + ( (end2-end1)* 1 );
                    var rotatedLine = Quaternion.AngleAxis( 90, anchor.transform.rotation*Vector3.up ) * end1;
                    //Debug.DrawLine(end2, rotatedLine, Color.blue);
                    var newLine1 = Instantiate(line );
                    LineRenderer lr2 = newLine1.GetComponent<LineRenderer>();
                    lr2.SetPosition(0,end1);
                    lr2.SetPosition(1,rotatedLine);

where anchor.transform.rotation*Vector3.up represents the normal of the plane. However this does not produce the required results

1 Answer 1

5

use Cross product to calculate perpendicular line:

if N is the plane normal, direction of perpendicular line is:

Vector3 dir = Vector3.Cross( N, ( end2 - end1 ).normalized );

and the line is defined by:

lr2.SetPosition( 0, end1 );
lr2.SetPosition( 1, end1 + dir );

(use Cross( ( end2 - end1 ).normalized, N ); for the opposite direction.

1
  • Thank you so much for the answer ,setting second position as "end1+ dir" was where I was going wrong
    – Kitwradr
    Jul 10, 2018 at 7:10

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.