Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to create a line using two Vector3 points in unity?

I know there exist some functions like lineRenderer etc, but I want to create a straight line in the scene using two points(in Vector3 form). I don't want to draw the line by using any key or using the mouse, I just want to see the line in the scene when I trigger some event or just after I click play button.

Can anyone help me?

like image 984
Harry Xiong Avatar asked Oct 07 '13 23:10

Harry Xiong


3 Answers

//For creating line renderer object
lineRenderer = new GameObject("Line").AddComponent<LineRenderer>();
lineRenderer.startColor = Color.black;
lineRenderer.endColor = Color.black;
lineRenderer.startWidth = 0.01f;
lineRenderer.endWidth = 0.01f;
lineRenderer.positionCount = 2;
lineRenderer.useWorldSpace = true;    
                
//For drawing line in the world space, provide the x,y,z values
lineRenderer.SetPosition(0, new Vector3(x,y,z)); //x,y and z position of the starting point of the line
lineRenderer.SetPosition(1, new Vector3(x,y,z)); //x,y and z position of the end point of the line
like image 93
Codemaker Avatar answered Nov 06 '22 20:11

Codemaker


Ok, I've figured it out by using LineRenderer like this:

var line: GameObject=GameObject.Find("/LineRenderer");
fence = Instantiate(line,Pos,Rotation);
fence.setPosition(0,p1);
fence.setPosition(1,p2);

Thanks for all of your answers above

like image 3
Harry Xiong Avatar answered Nov 06 '22 20:11

Harry Xiong


If you want a line in 3D space, try creating a LineRenderer, sample here: http://rockonflash.wordpress.com/2010/04/17/how-to-do-lasers-in-unity3d/

docs here: http://docs.unity3d.com/Documentation//Components/class-LineRenderer.html

For a 2D line (onGUI), try:

 function OnGUI () {
    GUIUtility.ScaleAroundPivot (Vector2(0.5, 0.5), Vector2(328.0, 328.0));
    GUI.Label (Rect (200, 200, 256, 256), textureToDisplay);
 }

there are other options presented in this discussion: http://forum.unity3d.com/threads/17066-How-to-draw-a-GUI-2D-quot-line-quot

like image 1
Stanley Avatar answered Nov 06 '22 20:11

Stanley