- Home /
LineRenderer GetPosition? not there.
hi i asked a question a few months ago here and someone said to use the getPosition and setPosition. but i looked in the lineRenderer class in the scripting reference and i could not find GetPosition.
is it possible that it was removed?
thanks.
Answer by Mike 3 · Jul 13, 2010 at 07:23 PM
The reply you quoted said that it wasn't possible with the built in LineRenderer, and instead said to extend the original class
Sounds like a good plan to me - Something like this should do it (in c# - my js is too rusty for inheritance):
using System; using UnityEngine;
public class AccessibleLineRenderer : LineRenderer { private Vector3[] vertices = new Vector3[0]{};
new public void SetPosition(int index, Vector3 position)
{
vertices[index] = position;
base.SetPosition(index, position);
}
new public void SetVertexCount(int count)
{
Array.Resize(ref vertices, count);
base.SetVertexCount(count);
}
public Vector3 GetPosition(int index)
{
return vertices[index];
}
}
I'm not sure js has the new syntax, so i'll write a js version without it
You'll need to use different function names for js, not sure you can shadow the functions properly without warnings. Alternatively, you could slap the c# version into Plugins or Standard Assets and it should work the same from js ;)
Cant use inheritance, it show me this error `CustomLineRenderer': cannot derive from sealed type `UnityEngine.LineRenderer'
Answer by alisator · Nov 20, 2012 at 09:07 AM
Hey, maybe I ll get the answer here...
private void axes()
{
Debug.Log ("start line");
Color c1 = Color.yellow;
Color c2 = Color.red;
int lengthOfLineRenderer = 20;
LineRenderer lineRenderer = gameObject.AddComponent<LineRenderer>();
lineRenderer.material = new Material(Shader.Find("Particles/Additive"));
lineRenderer.SetColors(c1, c2);
lineRenderer.SetWidth(0.01f, 0.9f);
lineRenderer.SetVertexCount(lengthOfLineRenderer);
for(int i = 0; i < lengthOfLineRenderer; i++)
{
Vector3 pos = new Vector3(i, 0.0f, 0f);
Vector3 pos2 = new Vector3(i,i,0.0f);
//lineRenderer.SetPosition(0,pos);
lineRenderer.SetPosition(10,pos2);
}
}
I tried different indexes, and hey? The line was rendered in different way - I just wanted have axis x and axis y, but I dot get, how the indexes working, and on unity website i found nothing... Do you know meaning of those indexes? Thx a lot!
the linerenderer.setposition() want 2 argument, first a integer as the index and then a vector3 as position. the index here is which point in line the position get set to, so if the index is 0, then it will be the first point of the line. and in your code index being 20 the last. the simples way of doing this is to use " lineRenderer.SetPosition(i,pos); " if you put this in a for loop which adds to i every time it loops through and the position had i in it then you would get a line. if you don't change " pos " in the loop you would just set a line which stays at the same point, which isn't what you want.
Your answer
Follow this Question
Related Questions
The name 'Joystick' does not denote a valid type ('not found') 2 Answers
Help With Add and Remove on a loop 2 Answers
Line Renderer Not Displaying in 2D Game Window 0 Answers
Remove all clips from animation by script 2 Answers
Laser Beam in Unity 0 Answers