How can I draw a line at a specific angle?

I want to draw two lines from the same origin but at specific angles relative to an original line (to form a field of view). How do I do this? In 2D space FWIW.

In the image below, the three lines (purple, 2x orange) would extend from the ‘eye’ transform at the tip of the triangle. The purple line is directly ‘up’ or ‘forward’ with the orange ones at 65 degrees on either side. The lines need to rotate with the parent transform.

The purple line is just a direct line drawn between the tip of the triangle and another child transform, which I achieve at runtime using a simple Debug.DrawLine.

40191-eyes.png

I’ve seen a few answers on here for similar queries, but they have not been exactly the same problem, and since I don’t understand quaternions very well I can’t adapt those answers to my problem.

I think Quaternion.AngleAxis is the key, but I can’t figure out what to give it to get the results I need.

I’ve tried the code given here: Draw line to a specific angle - Unity Answers, but it doesn’t seem to work (I don’t understand exactly what the code is doing so I can’t adapt it to my needs).

So I wrote a long thing about using the example you gave and Debug.DrawLine to accomplish your task. Then I realized that you probably don’t want to use Debug things in final code so that isn’t helpful. And then I realized that there are other easier solutions to your issue that I was ignoring because I was trying to explain Quaternion.AngleAxis.

Firstly tldr of what I initially wrote:

-in the Quaternion.AngleAxis example the transform.up is the vector to be rotated around so you would need Vector3.forward instead. Then multiply it by the line(he’s missing an *)

-the line vector just needs to be whatever is your local facing/forwards, so in 2D it’s probably transform.up because that’s what it looks like in your image(green gizmo line).

-assuming you are rotating the whole game object that should be correct it will probably be length 1 so you can multiply it by a float to change the length.

But you don’t really need to worry about that.

Actual Answers:

Since you are working in 2D you could just make a sprite image of the lines you need and make them child objects of your main gameObject. So that when it rotates they do as well, and are in the correct locations/rotation.

You can also do almost the same thing with the Line Renderer Unity - Manual: Line Renderer component

Just add a couple of these and set up the points to where you need them. Remember to uncheck Use World Space at the bottom of the component in the inspector. If you don’t they will just be stuck at the world space co-ordinates.

Hope that helps/works!