Question

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?

Was it helpful?

Solution 2

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

OTHER TIPS

//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

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

Another option that might work for your needs is to use a gizmo in your scene. Because Gizmos are applied in a separate matrix you can do a lot of fun stuff with them.

The basic:

void OnDrawGizmos ()
{
    Gizmos.color = new Color(1f, 0f, 0f, 0.5f);
    Gizmos.DrawLine(positionA, positionB);
}

Will get you there. Something that I've been using quite a bit lately however is to offset the gizmo matrix instead, and then render everything in a unit-space.

void OnDrawGizmos ()
{
        Matrix4x4 rotationMatrix = Matrix4x4.TRS(transform.position, transform.rotation, positionA - positionB);
        Gizmos.matrix = rotationMatrix;
        Gizmos.DrawWriteCube(Vector3.zero, Vector3.one);
}

Both are fun, but the second instance can help you later on when you start trying to represent content that is rotated or needs to be otherwise offset.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top