- Home /
How can I draw the diagonals of a screen?
I am trying to make a phone game and I need to split the screen in 4 sections by drawing the diagonals oof the screen. I looked over every tutorial on this site about drawing lines using LineRenderer and so far, that's the only way that I can use to draw a line wider than 1 pixel (I need it to be 3 pixels wide). The main problem is: I can only make the diagonal between the top-right corner and the bottom-left one show up when I build the app. If i run the game on my computer, through UnityRemote on my phone, both of the diagonals show up, but if I build it, only one gets drawn. This is the code I am using to draw the diagonal that is working:
using UnityEngine;
using System.Collections;
public class Lines1 : MonoBehaviour {
private LineRenderer line;
public Material material;
// Use this for initialization
void Start () {
line = new GameObject("Line").AddComponent<LineRenderer>();
line.material = material;
line.SetVertexCount(2);
line.SetPosition(0, new Vector3(Screen.width, Screen.height));
line.SetPosition(1, new Vector3(0, 0));
line.SetWidth(3, 3);
line.useWorldSpace = true;
}
// Update is called once per frame
void Update () {
}
}
Since I can't draw 2 lines with one LineRenderer, I created 2 objects, each one with a LineRenderer and 2 scripts, one for each line, to draw them. The only difference between the scripts are the lines where I set the position of the start point and the end point of the line. For example, in my 2nd script, I have
line.SetPosition(0, new Vector3(Screen.width,0)); line.SetPosition(1, new Vector3(0, Screen,height));
I will attach 2 photos of the game, with both of the scripts running, both on my computer and on my phone, after I build it.
This is how the game looks when I run it on my computer:
This is how the game looks on my phone, after I build the app (ignore the placement of the sprites and that text in the middle of the screen...they're just a few things I will take care of after I get over this problem):
I thought about using other methods, like GL.Begin(), but I am not too familiar with that, so it would be perfect if I can do everything with LineRenderers
$$anonymous$$aybe it's a problem with the normal of the invisible line?
Anyway, another option is to use the Drawing.cs available on the User wiki.
Answer by Hybrid-Dragon · Jul 24, 2016 at 08:43 AM
Hello,
First, your second line's script should be identical to the first line's but with either Screen.width or Screen.height as a negative (just simply have a - precede the one you want to flip).
Second, you can have two lines in the same script. Just use another private variable for the second line and, optionally, a second public material for the line. After that, copy the first line's script and paste it just after with the new name for the second line, as different items referenced in scripts can't have the same name.
Hello! Thanks for replying. I tried your solution, but with no success...I've tried writing all the code for the lines in one script before, and I had the same result, but I tried it again, and here's how it look now: using UnityEngine; using System.Collections; using System.Linq;
public class lines2 : $$anonymous$$onoBehaviour {
private LineRenderer line;
private LineRenderer line2;
public $$anonymous$$aterial material;
// Use this for initialization
void Start()
{ //this is the line that I can't see when I build the app
line2 = new GameObject("Line2").AddComponent<LineRenderer>();
line2.material = material;
line2.SetVertexCount(2);
line2.SetPosition(0, new Vector3(Screen.width, 0));
line2.SetPosition(1, new Vector3(0, Screen.height));
line2.SetWidth(3, 3);
line2.useWorldSpace = true;
line2.SetColors(Color.black, Color.black);
///this is the line that shows
line = new GameObject("Line").AddComponent<LineRenderer>();
line.material = material;
line.SetVertexCount(2);
line.SetPosition(0, new Vector3(Screen.width,Screen.height));
line.SetPosition(1, new Vector3(0, 0));
line.SetWidth(3, 3);
line.useWorldSpace = true;
line.SetColors(Color.black,Color.black);
}
// Update is called once per frame
void Update () {
}
}
The problem I had with putting a $$anonymous$$us sign ("-") before any Screen.height or Screen.width parameter is that it just makes the line pop out of the frame. I can see it in the scene view, but not in game view. I don't think the problem is linked to the position of the line's ends because I tried setting some random start and end points for the line that doesn't work, so I can see that the LineRenderer does it's job, and it did. I think the problem is with those 2 corners in particular, and I have no idea how that can happen.
Answer by AceOneFlow · Jul 31, 2016 at 05:35 PM
I have solved the problem. I couldn't draw 2 actual lines using LineRenderer, so I am creating two different cubes, I modify their dimensions and i position them diagonally so they both look like 2 diagonal lines. It's a lot easier this way, in my opinion.