- Home /
How to set indirect relation to key, value pairs, study case of a line renderer?
Hello everybody,
My question is about setting opposite indexes (indirect relations) for a multidimensional array of line renderer position points, as the key: position index, and the value: position vector3
the way it works, once a line is drawn line renderer component shows a list of key, value pair with indexes going from 0 - position count, and the added points (vector3).
For example, a line with three points would be (as the position count index is the first number, followed by vector3):
0, (-0.5, 0, 1)
1, (0.5, 0, 1)
2, (1.5, 0, 1)
My goal is to revert the order, such that:
0, (1.5, 0, 1)
1, (0.5, 0, 1)
2, (-0.5, 0, 1)
As you can see the index remains the same, the order of the vectors is reversed (this is also called indirect relations).
This is what I've tried so far - but it's not working well:
I've created a list of Vector3 positions, and populated it with the points above. The code is:
for (int i = 0; i < positions.Count; i++) {
lineRenderer.SetPosition(lineRenderer.positionCount - i, positions[i]);
}
However, on the first point I get an error about a negative linerenderer.positionCount and the point at position(0) = (0 , 0 , 0)
So I think this is not a correct code.
Thanks
Answer by CodesCove · Jun 23, 2020 at 08:01 PM
Two things: 1) insert -1 to the SetPosition --> lineRenderer.SetPosition(lineRenderer.positionCount - i -1, positions[i]); 2) Make sure the positionCount is correct.. if you set the vertices in code then set the positionCount to the correct number in code (for example SetPositions require this before setting the actual positions..)