Draw a line that will shoot straight from all the vertices of my shape to basically the edge of the screen?

by user113458   Last Updated March 13, 2018 07:13 AM

How can I draw a line that will shoot straight from all the vertices of my shape to basically the edge of the screen?

All of the vertices are vector 3 arrays.

Something like this (red line is the line I want to create): enter image description here

Tags : unity


Answers 1


public class Line
{
    Vector2 point0; //startpoint
    Vector2 point1; //endpoint
}

//...set up line0 and line1 as you currently do...

//get difference between end and start points for each line
Vector2 line0Diff = line0.point1 - line0.point0; 
Vector2 line1Diff = line1.point1 - line1.point0; 

//sum these differences to get diff of resultant line
Vector2 line2Diff = line0Diff + line1Diff;

//Get the actual points making up the resultant line
Line line2 = new Line();
line2.point0 = line0.point1; //start of this line is end of one of the original lines
line2.point1 = line0.point1 + line2diff;

In order for this to work, remember that vectors have direction. In that much, they are not the same as lines (your line is described by two vectors acting as points). Thus if you want the vectors in the first diagram to create the red vector, both black and orange must be facing roughly toward to the north, i.e. their .point1 must be near the top; in your second diagram both black vectors must be facing roughly toward the west, i.e. their .point1 must be near the right.

As for getting the point at the edge of the screen, you will need Camera.ScreenToWorldPoint(). Now, to make the resulting red vector to be a specific length to reach to the edge of the screen, you must normalize it (bring it down to a length of 1.0) first, then multiply it by the length you desire. That length will be determined by your use of ScreenToWorldPoint().

Arcane Engineer
Arcane Engineer
March 13, 2018 06:26 AM

Related Questions


How to animate objects in Unity 3D

Updated July 12, 2015 13:05 PM