Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Debug.DrawLine not showing in the GameView

Tags:

c#

unity3d

unity5

I'm working on a 2D Unity app and I'm encountering some weird behavior.

This code works just fine.

Debug.DrawLine(button1.transform.position, button2.transform.position, Color.green);

When I run the app, I see a green line in the Scene view.

But nothing appears in the Game view when I have the following line.

Physics2D.Linecast(button1.transform.position, button2.transform.position);

I'm confused as to how Unity is able to draw a line between these two buttons, but for some reason, it's just not doing it in the Game view.

Any idea how I'd troubleshoot this?

like image 736
Ben Downey Avatar asked Mar 15 '17 19:03

Ben Downey


People also ask

Can you draw in unity?

Yes, it is possible. Highly performant Graphic API usually do not provide functions to draw a circle. Instead, they produce functions to draw lines, quads and triangles, and you can use those as building blocks for making approximation of desired shape.


1 Answers

Just line Serlite said, Physics2D.Linecast is not used to draw a line but to detect if there is an object in the middle of two objects with raycast. It has nothing to do with drawing lines.

As you already know, Debug.DrawLine will only work in the Scene view unless Gizmos is enabled. You can just LineRenderer or the GL functions to draw lines which will work without enabling Gizmos and will also work in a build.

Here is a helper class for drawing line in the Game and Scene view.

public struct LineDrawer
{
    private LineRenderer lineRenderer;
    private float lineSize;

    public LineDrawer(float lineSize = 0.2f)
    {
        GameObject lineObj = new GameObject("LineObj");
        lineRenderer = lineObj.AddComponent<LineRenderer>();
        //Particles/Additive
        lineRenderer.material = new Material(Shader.Find("Hidden/Internal-Colored"));

        this.lineSize = lineSize;
    }

    private void init(float lineSize = 0.2f)
    {
        if (lineRenderer == null)
        {
            GameObject lineObj = new GameObject("LineObj");
            lineRenderer = lineObj.AddComponent<LineRenderer>();
            //Particles/Additive
            lineRenderer.material = new Material(Shader.Find("Hidden/Internal-Colored"));

            this.lineSize = lineSize;
        }
    }

    //Draws lines through the provided vertices
    public void DrawLineInGameView(Vector3 start, Vector3 end, Color color)
    {
        if (lineRenderer == null)
        {
            init(0.2f);
        }

        //Set color
        lineRenderer.startColor = color;
        lineRenderer.endColor = color;

        //Set width
        lineRenderer.startWidth = lineSize;
        lineRenderer.endWidth = lineSize;

        //Set line count which is 2
        lineRenderer.positionCount = 2;

        //Set the postion of both two lines
        lineRenderer.SetPosition(0, start);
        lineRenderer.SetPosition(1, end);
    }

    public void Destroy()
    {
        if (lineRenderer != null)
        {
            UnityEngine.Object.Destroy(lineRenderer.gameObject);
        }
    }
}

Usage:

LineDrawer lineDrawer;

void Start()
{
    lineDrawer = new LineDrawer();
}

void Update()
{
    lineDrawer.DrawLineInGameView(Vector3.zero, new Vector3(0, 40, 0f), Color.blue);
}

When done, you can call lineDrawer.Destroy();.

like image 131
Programmer Avatar answered Oct 02 '22 09:10

Programmer