Hello, I want to calculate the distance between the list of game objects So element will check its distance with every other element and so on, I am doing this with following code it works but not efficient enough. Can anyone tell me the correct way to use loops in job system.
struct Field: IJobParallelForTransform
{
public NativeArray<float> Dist;
public Vector3 PlayerPos;
public void Execute(int i, TransformAccess transform)
{
Dist[i] = Vector3.Distance(PlayerPos, transform.position);
}
}
public List<Transform> Childrens;//All gameobjects list
public TransformAccessArray ALlObjects;
NativeArray<float> distances;
void Start ()
{
ALlObjects = new TransformAccessArray(0, -1);
distances = new NativeArray<float>(Childrens.Count, Allocator.Persistent);
}
void Update()
{
for (int i = 0; i < Childrens.Count; i++)
{
Field job1 = new Field()
{
Dist = distances,
PlayerPos= Childrens[i].position,
};
JobHandle jH = job1.Schedule(ALlObjects);
jH.Complete();
for (int j = 0; j < distances.Length; j++)
{
if (distances[j] <= 0.8f && distances[j] > 0)
{
break;
}
if (distances[j] > 0.8f )
{
}
}
}
}