Hi,
i have a problem where i really cant find it’s source.
i do something like this:
(pseudocode)
List<Vector3> list = new List<Vector3>();
void AddPosition(Vector3 ApositionVector) { //called every second
Debug.Log("Positions added");
list.Add(ApositionVector);
if (list.Count > 2) {
list.RemoveAt(0);
}
t = 0;
}
float t = 0;
Vector3 interPosition;
void Update() {
t += Time.deltaTime; Edit: (or t+=0.1f; }
if (list.Count == 2) {
interPosition = Vector3.Lerp(list[0], list[1], t);
}
}
Now what happens is, interPosition is at the correct position,
but only about roughly one in two or three frames, the rest of the time it snaps to (0,0,0). And i cant figure out why.
Edit: Update: list.Count reports 0 every second frame (then 2 again), even though i’m not changing the list in between.
This is really bugging me. I have no explanation for it.
I tested everything with Debug.Log and wrapping list.add and remove to see when it’s called,
but there is nothing wrong except the odd count reporting.
The list is not public so i dont expect there to be a serialization error or something (i even tried adding the nonSerializabeAttribute, though i’m not sure whether i really know what serializing means in Unity/programming terms).
You say AddPosition is called once per second. You reset t to 0 in this method, and you increment by time.deltaTime in the update step. You’re not guaranteeing a continuous gradient of “t” this way. You probably can get away with subtracting t by 1 in AddPosition to guarantee smooth interpolation. Though you should actually subtract by the precise time step between successive AddPosition calls.
void AddPosition(Vector3 ApositionVector) { //called every second
Debug.Log("Positions added");
list.Add(ApositionVector);
if (list.Count > 2) {
list.RemoveAt(0);
}
t = t - 1; // Since AddPosition is being called every 1 second, then subtract t by 1.
}
Actually, in my real non pseudo script i have time.deltaTimespeedupdateRate.
But i would have explain those if i included them in the example.
But: that is not the problem, i think, as the lerp looks very smooth ( every even frame, so not really see-able. I need clean values though).
But List.Count returning 0 every second frame is the culprit here i suspect.
(by //called every second, i meant to express, that this function is not called every frame, when it really is called every updateRate, which i can vary(, but i thought that wouldn’t give any more information, regarding the problem) )
Also if i subtracted -1 from t, t would most probably never return to the 0-1 range as i increment it continuously (given, that AddPosition is also not called exactly every second ).
List.Count returns the number of items in the list. If it returns 0, then you have 0 items in the list. The end.
It’s likely you have multiple identical scripts attached or something along those lines.
–Eric
Sometimes you need someone to point out the obvious…
I feel stupid now.
The code is part of a movement script which is attached to npc’s + the player, with a switch (isNpc), which was not set, so multiple instances ran that part.
Thanks for helping.
I would mark the topic as solved, i just dont know how.
There isn’t really a “mark as solved” function on the forums, but glad I could help! This is a pretty common occurrence so don’t feel bad.
–Eric