- Home /
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.
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: http://answers.unity3d.com/questions/42300/draw-line-to-a-specific-angle.html, 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).
$$anonymous$$aybe you can use Quaternion.Euler if you understand angles?
Edit the link, there's an unwanted appended comma there.
Also a note: you can use vectors ins$$anonymous$$d of angles to draw lines.
Answer by daneislazy · Feb 05, 2015 at 02:44 PM
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 http://docs.unity3d.com/Manual/class-LineRenderer.html
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!
Your answer
Follow this Question
Related Questions
Finding the angle between 2 clicked points 2 Answers
follow an object's x-axis and have it translate smoothly into another object's z-axis rotation 1 Answer
Camera direction depending on targets slope (X axis) 0 Answers
How to create a 2D line between a Game Object and the mouse? 0 Answers
Quaternion and rotation angles 2 Answers