- Home /
How to draw a line in unity3d
I need a little help in drawing a line. I want to able to draw a line in the game with my mouse as I click and drag extended the certain length of the line. Also, I want to have the line disappear after a few seconds or after it collides with an object. Is there a way for me to draw a line with my mouse? I am not sure if the line renderer would work in this case.
Answer by Wuzseen · Mar 04, 2013 at 08:00 AM
public LineRenderer lineRender;
private int numberOfPoints = 0;
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
if( Input.GetKey( KeyCode.Mouse0 ) ) {
numberOfPoints++;
lineRender.SetVertexCount( numberOfPoints );
Vector3 mousePos = new Vector3(0,0,0);
mousePos = Input.mousePosition;
mousePos.z = 1.0f;
Vector3 worldPos = Camera.main.ScreenToWorldPoint(mousePos);
lineRender.SetPosition(numberOfPoints - 1, worldPos);
}
else {
numberOfPoints = 0;
lineRender.SetVertexCount(0);
}
}
So I whipped this up. To answer your question: lineRenderers will do everything just fine. You simply draw a vertex at your mouse every frame while you have it held down. I have it make the line disappear by resetting the vertex count back to 0, but you can make this disappear however you wish. Feel free to customize the line renderer however you wish, but this code should take care of everything. It translates your mouse into a world coordinate and draws a new vertex of the line renderer there.
Thanks for the code. I have some questions about it. First, when I use the script, should I attach it to an empty GameObject? Once I use the script, can I draw a line in the game scene? I attached the script to an empty GameObject and clicked my mouse and nothing happens.
Second, on the drawline script, where it says "Line Renderer," it looks like it is looking for an object. Is that the part where it is asking for the an object to put as the line?
Yes, attach it to an empty gameobject. The reason it probably isn't drawing anything is that you haven't linked the line renderer in your inspector: http://imgur.com/laDAGLm Notice the blue line showing you what to link up.
Also, I suggest making the start and ending width of the line renderer a lot smaller than 1. (1 is really, really, big)
Answer by BCGray00 · Sep 05, 2014 at 05:39 PM
#pragma strict
var startx : int;
var starty : int;
static var startv3 : Vector3;
var currentx : int;
var currenty : int;
static var currentv3 : Vector3;
var endx : int;
var endy : int;
static var endv3 : Vector3;
function Update(){
if(Input.GetMouseButtonDown(0)){
startx = Input.mousePosition.x;
starty = Input.mousePosition.y;
startv3 = Camera.main.ScreenToWorldPoint(Vector3(startx, starty, 2));
}
if(Input.GetMouseButton(0)){
currentx = Input.mousePosition.x;
currenty = Input.mousePosition.y;
currentv3 = Camera.main.ScreenToWorldPoint(Vector3(currentx, currenty, 2));
GetComponent(LineRenderer).SetPosition(0, startv3);
GetComponent(LineRenderer).SetPosition(1, currentv3);
}
if(Input.GetMouseButtonUp(0)){
endx = Input.mousePosition.x;
endy = Input.mousePosition.y;
endv3 = Camera.main.ScreenToWorldPoint(Vector3(endx, endy, 2));
GetComponent(LineRenderer).SetColors(Color.black, Color.black);
GetComponent(LineRenderer).SetPosition(0, startv3);
GetComponent(LineRenderer).SetPosition(1, endv3);
}
}
Your answer
Follow this Question
Related Questions
Drawing a line in a sprite 0 Answers
Vectrosity : Adding more anchor and control points 0 Answers
Line Collision Detection for 2D Game 1 Answer
Line renderer spring shape 1 Answer
How can I Disable all but one renderer in an array? 1 Answer