Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Unity3d Linerenderer not visible

Tags:

unity3d

unity5

I am trying to draw a line between two UI GameObjects with Linerenderer. In scene mode everything work fine, but in game mode line is invisible. I tried to change Z position of objects but lines are still invisible. Can anyone help me? Thanks in advance

private LineRenderer lineRenderer;
private float counter;
private float dist;
private Vector3 aPos;
private Vector3 bPos;
public Transform origin;
public Transform destination;
public float lineDrawSpeed = 6f;

// Use this for initialization
void Start()
{
    lineRenderer = GetComponent<LineRenderer>();
    aPos = new Vector3(origin.position.x, origin.position.y, origin.position.z); // Using these to move the lines back
    bPos = new Vector3(destination.position.x, destination.position.y, destination.position.z);

    lineRenderer.SetPosition(0, aPos);
    lineRenderer.SetWidth(3f, 3f);

    dist = Vector3.Distance(origin.position, destination.position);
}

// Update is called once per frame
void Update()
{

    if (counter < dist)
    {
        counter += .1f / lineDrawSpeed;

        float x = Mathf.Lerp(0, dist, counter);

        Vector3 pointA = aPos;
        Vector3 pointB = bPos;

        Vector3 pointAloneLine = x * Vector3.Normalize(pointB - pointA) + pointA;

        lineRenderer.SetPosition(1, pointAloneLine);
    }

}
like image 369
Alexander Kucherenko Avatar asked Nov 11 '16 18:11

Alexander Kucherenko


2 Answers

Unless I'm overlooking some logic error in the code you've posted, I think the problem might be with the material.

Generic debugging help for line renderers:

Try setting the color/material of the line renderer:

lineRenderer.sortingOrder = 1;
lineRenderer.material = new Material (Shader.Find ("Sprites/Default"));
lineRenderer.material.color = Color.red; 

If that doesn't work, perhaps you need to specify the number of vertexes manually?

mineLaser.SetVertexCount (2);

Finally, if these both don't work, it might just be a logic error; try setting the transforms for the lineRenderer's position to be some predefined value and see if it shows up.

For this specific question:

Ah, so its on a canvas. Assuming you mean the UI canvas, I believe linerenderer is the wrong tool to use in this situation. Check out this question.

One of the answers there suggests to:

just use a panel filled with any color you want and use Height and Width to set the length and the Width of your line

like image 94
code11 Avatar answered Oct 04 '22 08:10

code11


This is impossible in "Screen Space - Overlay" Canvas mode. In that mode UI overlay draws on top of everything in Scene (including LineRenderer, that actually non UI element).

Try to use "Screen Space - Camera" option for your Canvas and "Use World Space" option for you Line Renderer.

like image 38
Anatoly Nikolaev Avatar answered Oct 04 '22 08:10

Anatoly Nikolaev