- Home /
How to draw a line using script
How can I draw a line using script?
Hey guys, looking for ways to draw a line from my camera till one hit point reaches this post , where LineRenderer insinuates , I made this $$anonymous$$i me function that draws the line, you just have to pass a Vector3 Vector3 start and end , color and duration , everything is very basic just have to stick these two functions to your code and run it with the click, maybe someone will serve as me.
example to use
if (Input.Get$$anonymous$$ouseButton(0)) {
RaycastHit hit;
if (Physics.Raycast(ray, out hit)){
draw$$anonymous$$yLine(Camera.main.transform.position , hit.point, Color.yellow,1f);
}
void draw$$anonymous$$yLine(Vector3 start , Vector3 end, Color color,float duration = 0.2f){
StartCoroutine( drawLine(start, end, color,duration));
}
IEnumerator drawLine(Vector3 start , Vector3 end, Color color,float duration = 0.2f){
GameObject myLine = new GameObject ();
myLine.transform.position = start;
myLine.AddComponent<LineRenderer> ();
LineRenderer lr = myLine.GetComponent<LineRenderer> ();
lr.material = new $$anonymous$$aterial (Shader.Find ("Particles/Additive"));
lr.SetColors (color,color);
lr.SetWidth (0.1f,0.1f);
lr.SetPosition (0, start);
lr.SetPosition (1, end);
yield return new WaitForSeconds(duration);
GameObject.Destroy (myLine);
}
Answer by robert · Nov 27, 2009 at 08:56 AM
You can either use a LineRenderer (also from a script) for a line drawn on a camera-facing stripe of quads or you can use good'ol GL.Begin(GL.LINES) to have the traditional rasterization to a line of pixels.
Note that the GL class is only available with Unity Pro, and doesn't work on the iPhone.
GL Class is working in free version unity 4.0 and higher
Answer by paranoidray · Dec 06, 2015 at 08:55 PM
void DrawLine(Vector3 start, Vector3 end, Color color, float duration = 0.2f)
{
GameObject myLine = new GameObject();
myLine.transform.position = start;
myLine.AddComponent<LineRenderer>();
LineRenderer lr = myLine.GetComponent<LineRenderer>();
lr.material = new Material(Shader.Find("Particles/Alpha Blended Premultiply"));
lr.SetColors(color, color);
lr.SetWidth(0.1f, 0.1f);
lr.SetPosition(0, start);
lr.SetPosition(1, end);
GameObject.Destroy(myLine, duration);
}
Simple and easy to use code.
I got cought out with this, my own fault. I wasnt exporting the shader so need to make sure that the Shader.Find("Particles/Alpha Blended Premultiply") is set within the unity environment so it gets compiled with the release build. Or if not make sure you are exporting the shader with the release build.
so for clarity for $$anonymous$$e i did:
public Shader lineShader;
and then in the editer selected the shader I wanted
Unable to add separate line my script is :
if (((Input.touchCount > 0 && Input.GetTouch(0).phase == TouchPhase.$$anonymous$$oved) || Input.Get$$anonymous$$ouseButton(0))) { Plane objPlane = new Plane(Camera.main.transform.forward * -1, this.transform.position); Ray mRay = Camera.main.ScreenPointToRay(Input.mousePosition); float rayDistance; if (objPlane.Raycast(mRay, out rayDistance)) this.transform.position = mRay.GetPoint(rayDistance); }
SetColors and SetWidth are deprecated. Good answer, though.
Answer by Goody! · Nov 28, 2009 at 12:27 AM
A simple one is debug.drawline.
Below from: http://unity3d.com/support/documentation/ScriptReference/Debug.DrawLine.html
Debug.DrawLine
static function DrawLine (start : Vector3, end : Vector3, color : Color = Color.white) : void Description
Draws a line from the point start to end with color.
The line will be drawn in the scene view of the editor. If gizmo drawing is enabled in the game view, the line will also be drawn there.
// Draws a red line from the the world-space origin to the point (1, 0, 0)
function Update () {
Debug.DrawLine (Vector3.zero, new Vector3 (1, 0, 0), Color.red);
}
Btw, in case you didn't know, a vector3 is a point in space representing x,y,z.
Answer by crystalfish · Oct 29, 2013 at 10:16 AM
Gizmos.DrawLine() , Debug.DrawLine(),GL.Begin(GL.LINES),or handls.Drawline().
Too often overlooked. Great for quick debugging in game view.
Answer by ericksson · Nov 27, 2009 at 08:59 AM
There is a discussion about this here: http://forum.unity3d.com/viewtopic.php?t=34735&sid=b7db7a2f8b373969ba16f82a6af23b4a
The script discussed above is here: http://www.unifycommunity.com/wiki/index.php?title=DrawLine
It might not work for complex drawing cases, but it might work for what you need.
Your answer
Follow this Question
Related Questions
What is the easiest way to draw a line? 1 Answer
Free script/plugin for drawing a line with a collider? 2 Answers
Draw line within bounds of circular collider 0 Answers
Draw line from one point to another using mouse 0 Answers
Rendering a spring joint 1 Answer