- Home /
error in for-loop updating vector3
I cannot find the error, which does not update the vector3 of each created point.
The idea is to create two vertices on each click that you press ..
the error is that the vertices are not updated, and always remain in the first point.
If I use break, it updates me in an orderly manner without triggering the amount of verts, wildly. (2,4,6,12,20,30,42 etc ...)
If I use "continue" the effect is created as desired, but the verts and norms are loaded erroneously and above the values as mentioned before.
I leave the code:
for (int i = 0; i < nodes.Count; i++) {
Vector3 perpendicularDirection = new Vector3(-( nodes[i+1].z-nodes[i].z ), nodes[i].y, nodes[i+1].x -nodes[i].x).normalized;
verts.Add (nodes[i] + perpendicularDirection*road.Width);
verts.Add (nodes[i] + perpendicularDirection*-road.Width);
norms.Add (Vector3.up);
norms.Add (Vector3.up);
break;
}
Answer by Bunny83 · Jul 31, 2019 at 08:46 AM
Sorry but your description is very confusing. First of all you do not update the vertices but you add all of them each time you call that loop. It's not clear if you first clear the verts and norms lists or not. However from your count growth it looks like you don't clear them. Since you generate all points each time, you have to clear the lists before you add the points again.
Though you have another issue which will case an index out of bounds exception. You can not do nodes[i+1]
since you iterate up to the last index. If you do +1 on the last index, the index will be out of bounds. Since you calculate the direct from one segment you are always one short. If you have 4 nodes (0 - 3) you only have 3 segments 0 to 1, 1 to 2 and 2 to 3. You either have to reuse the direction of the last segment for the last node or you need a different way to determine the direction..
The perpendicular would also make more sense if calculated from the tangent at that point which would be the average between the two directions at that point. Of course the first and last point only have one segment.
edit
Something like this:
verts.Clear();
norms.Clear();
int count = nodes.Count;
for (int i = 0; i < count; i++)
{
Vector3 dir = Vector3.zero;
if (i > 0)
dir += (nodes[i] - nodes[i - 1]).normalized;
if (i < count-1)
dir += (nodes[i + 1] - nodes[i]).normalized;
if (dir.sqrMagnitude > 0)
dir = dir.normalized;
Vector3 perpendicularDirection = new Vector3(-dir.z, 0, dir.x);
verts.Add (nodes[i] + perpendicularDirection * road.Width);
verts.Add (nodes[i] + perpendicularDirection * -road.Width);
norms.Add (Vector3.up);
norms.Add (Vector3.up);
}
I'm looking to do something similar from editor: link text
Your answer
![](https://koobas.hobune.stream/wayback/20220612205801im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
Order of instantiated objects in hierachy 3 Answers
For loop resetting itself, but needs to stop 2 Answers
ScrollableList best way to implement 0 Answers