Im drawing powerlines and the lines shows up in editor mode but not when game is running. Why?
using UnityEngine;
using System.Collections;
public class PowerLines : MonoBehaviour {
public Material lineMat;
public GameObject mainPoint;
public GameObject[] points;
void DrawConnectingLines() {
if(mainPoint && points.Length > 0) {
// Loop through each point to connect to the mainPoint
foreach(GameObject point in points) {
Vector3 mainPointPos = mainPoint.transform.position;
Vector3 pointPos = point.transform.position;
GL.Begin(GL.LINES);
lineMat.SetPass(0);
GL.Color(new Color(lineMat.color.r, lineMat.color.g, lineMat.color.b, lineMat.color.a));
GL.Vertex3(mainPointPos.x, mainPointPos.y, mainPointPos.z);
GL.Vertex3(pointPos.x, pointPos.y, pointPos.z);
GL.End();
}
}
}
// To show the lines in the game window when it is running
void OnPostRender() {
DrawConnectingLines();
}
// To show the lines in the editor
void OnDrawGizmos() {
DrawConnectingLines();
}
}
How? as the code are like now i need 1 script per 7 lines, or it bugs out and drawing lines or something when moving the mouse in the editor.
EDIT: what i do now is that i have 1 script on the object 1, it draws the lines to object 2 that have the same script that then draw lines to object 3 etc.
And since i need it on the camare, i dont know how to do that.
Just grab all the objects you need (GameObject.FindGameObjectsWithTag?) and draw them together. We’ll do our best to help if you explain what part you’re struggling with.
Im sorry, i didnt understand what you mean there, grab the lines or the objects where the lines will be draw between in to just 1 script?
And im a bit confused here lol.
And the lines is beeing drawn from sphere to sphere (7), and they are a child of the power line. witch means if i have for example 40 power lines i will have 280 lines and objects(spheres) in the script.
Seams like a time consuming scripting lol
All the objects. Drawing it all from one place would actually be more efficient than having 40 different scripts have their OnPostRender() called.
I whipped you up a quick example:
Code:
public class DrawPowerLines : MonoBehaviour
{
public GameObject[] poles;
protected Material lineMaterial;
protected void Start()
{
poles = GameObject.FindGameObjectsWithTag("Pole");
}
protected void OnPostRender()
{
if(poles.Length < 2)
return;
lineMaterial.SetPass(0);
GL.Begin(GL.LINES);
GL.Color(Color.black);
for(int i = 0; i < poles.Length - 1; ++i)
{
Transform current = poles[i].transform;
Transform next = poles[i + 1].transform;
// "left" sphere
GL.Vertex3(current.position.x - 2, current.position.y + 6, current.position.z);
GL.Vertex3(next.position.x - 2, next.position.y + 6, next.position.z);
// "right" sphere
GL.Vertex3(current.position.x + 2, current.position.y + 6, current.position.z);
GL.Vertex3(next.position.x + 2, next.position.y + 6, next.position.z);
}
GL.End();
}
}
To guarantee draw order, instead of using GameObject.FindGameObjectsWithTag(), you could also just manually add the game objects to the array via the editor (drag and drop).
Your script works perfect. How ever i think my models offset (center) aint in the center of the model, so the lines position where it thinks the centre are are way off the actually pylon.
I have to try the way you did on that picture. You said i could manually add the objects, but when i do, the script keep removing the object on start and mix the order they should be in, and also i had to make the material public since no material would be added.
I have 1 more question, if the next pylon are -10 lower or higer will the lines still been drawn to the right positions? why i ask is because when i started the game, after the lines was on the last pylon, the line went down to the terrain.
Just change around the numbers to accurately match your models. It shouldn’t matter if one is higher or lower than the next one, but it will matter if they’re rotated as my code doesn’t attempt to take that into account.