I tried to refactor the code inside the Entities.ForEach using method extracting. E.g. MoveToDestination (see image). This caused the entities to stop moving, even though it was actually the exact same code. Why is that? What are good practices to keep code clean/readable in this part?
public class MoverToDestinationSystem : SystemBase
{
protected override void OnUpdate()
{
float deltaTime = Time.DeltaTime;
Entities.ForEach((ref Translation translation, ref Rotation rotation, in Destination destination, in MovementSpeed speed) =>
{
if (ReachedDestination(translation, destination)) { return; }
float3 toDestinationDirection = destination.Value - translation.Value;
rotation.Value = quaternion.LookRotation(toDestinationDirection, new float3(0, 1, 0));
float3 movementToDestination = math.normalize(toDestinationDirection) * speed.Value * deltaTime;
MoveToDestination(translation, destination, toDestinationDirection, movementToDestination);
}).ScheduleParallel();
}
private static void MoveToDestination(Translation translation, in Destination destination, float3 toDestinationDirection, float3 movementToDestination)
{
if (math.length(movementToDestination) >= math.length(toDestinationDirection))
{
translation.Value = destination.Value;
}
else
{
translation.Value += movementToDestination;
}
}
private static bool ReachedDestination(Translation translation, Destination destination)
{
return math.all(destination.Value == translation.Value);
}
private static void FaceDestination(Rotation rotation, float3 toDestination)
{
rotation.Value = quaternion.LookRotation(toDestination, new float3(0, 1, 0));
}
}
[image]