- Home /
LineRenderer not showing the correct color.
I'm trying to create a pointer from an object, I want it red (or blue in the future). But the line is always black(ish).
I've been trying with the start colors and the materials without success.
My current code:
LineRenderer lineRenderer;
RaycastHit hit;
public Color c1;
public Color c2;
//public Material mat;
void Start() {
lineRenderer = gameObject.AddComponent();
lineRenderer.useWorldSpace = false;
lineRenderer.SetVertexCount(2);
lineRenderer.SetColors(c1, c2);
lineRenderer.SetWidth(0.1f, 0.1f);
//lineRenderer.material = mat;
lineRenderer.material.color = Color.red;
renderer.receiveShadows = false;
renderer.castShadows = false;
}
void Update () {
Physics.Raycast(transform.position,transform.forward, out hit);
if(hit.collider){
Vector3 dist = new Vector3(0,0,hit.distance);
lineRenderer.SetPosition(1, dist);
}
else{
Vector3 dist = new Vector3(0,0,500);
lineRenderer.SetPosition(1, dist);
}
}
The public colors, both are red.
I also tried to assing a public material (a simple red color) to no avail.
I've seen other people with similar problems but no solution (http://forum.unity3d.com/threads/57886-Unity-3-LineRenderer-problem)
Please help.
The code in the question never sets the position for the zero-th vertex in the line.
Thank you Graham, I'll check on that tonight, it seems to be the reason.
It turned out my problem was not the same. I had the ambient lighting of my project turned down (render settings) which also causes the line renderer to appear black. Adding a Self-illu$$anonymous$$ating shader will allow for colors to appear on the line renderer.
Answer by Zimorgh · May 09, 2013 at 03:06 AM
Yes, apparently the position zero changed the looks of the color, I still see it a bit darker, either red or blue. Thank you Graham.
Here's the code as it last worked:
LineRenderer lineRenderer;
RaycastHit hit;
Vector3 cero;
void Start() {
lineRenderer = gameObject.AddComponent();
lineRenderer.useWorldSpace = false;
lineRenderer.SetVertexCount(2);
lineRenderer.SetWidth(0.1f, 0.1f);
lineRenderer.material.color = Color.blue;
renderer.receiveShadows = false;
renderer.castShadows = false;
cero = new Vector3(0,0,0);
}
// Update is called once per frame
void Update () {
lineRenderer.SetPosition(0, cero);
Physics.Raycast(transform.position,transform.forward, out hit);
if(hit.collider){
Vector3 dist = new Vector3(0,0,hit.distance);
lineRenderer.SetPosition(1, dist);
}
else{
Vector3 dist = new Vector3(0,0,500);
lineRenderer.SetPosition(1, dist);
}
}