So I have the following code like this
protected override void OnUpdate()
{
var deltaTime = Time.deltaTime;
foreach (var e in GetEntities<Components>())
{
if (e.Path.Coords == null || e.Path.Coords.Count == 0) continue;
var target = e.Path.Coords.Peek();
var tolerance = e.Path.Roads.Peek().Width;
var coord = e.Position.Coordinate;
if (coord.X != target.X)
{
coord.X = target.X > coord.X
? Mathf.Clamp(coord.X + deltaTime, coord.X, target.X)
: Mathf.Clamp(coord.X - deltaTime, target.X, coord.X);
}
/* Code is repeated for Y and Z */
if (coord.Equals(target, tolerance))
{
e.Path.Coords.Pop();
e.Path.Roads.Pop();
}
// I believe this is redundant when coordinate is a class
e.Position.Coordinate = coord;
e.Transform.position = coord.ToVector3(1.05f);
}
}
When Coordinate is a class, this code breaks when there are more than two in the GetEntities…, It compiles but will get stuck in a loop where it assigns coord.X a value, but on the next loop I check it is back to the original, but it works fine when it is a struct.
Evidently I’m using the new Unity ECS. To my knowledge, this result shouldn’t matter in regular code, but for some reason it’s being wacky. Any ideas on why it does this? Or am I just having a brain fart and it also happens outside of ECS?