public void Execute(int index, TransformAccess transform) { if (!previousCubes[index].isAlive) { return; } int amount = 0; for (int i = 0; i < previousCubes.Length; i++) { if (!previousCubes[i].isAlive) { continue; } if (Vector3.Distance(transform.position, previousCubes[i].position) <= radius) { amount++; } } NativeArray <int> neighbors = new NativeArray <int>(amount, Allocator.Temp); for (int i = 0; i < previousCubes.Length; i++) { if (!previousCubes[i].isAlive) { continue; } if (Vector3.Distance(transform.position, previousCubes[i].position) <= radius) { neighbors[--amount] = i; } } Vector3 speed = moveToCentreMass(index, neighbors) + moveFromNeighbors(index, neighbors) + moveToSpeedCentreMass(index, neighbors) + moveToPlace(index); speed = LimitSpeed(speed, 3.5f); if (Vector3.Distance(transform.position, place) <= 10.0f) { currentCubes[index] = new cubeInfo(false, previousCubes[index].position, Vector3.zero); } else { transform.position += speed * deltaTime * 5; currentCubes[index] = new cubeInfo(true, transform.position, speed); } neighbors.Dispose(); }
// Start is called before the first frame update void Start() { place = GameObject.Find("OptimizedTree").transform.position; cubes = new List <GameObject>(); for (int i = 0; i < 200; i++) { GameObject instance = Instantiate(cubePrefab, cubePrefab.transform.position + new Vector3((float)(2 * i), 0.0f, 0.0f), transform.rotation) as GameObject; cubes.Add(instance); } Transform[] ttransforms = new Transform[cubes.Count]; previousCubes = new NativeArray <cubeInfo>(cubes.Count, Allocator.Persistent, NativeArrayOptions.UninitializedMemory); for (int i = 0; i < cubes.Count; i++) { previousCubes[i] = new cubeInfo(true, cubes[i].transform.position, Vector3.zero); ttransforms[i] = cubes[i].transform; } transforms = new TransformAccessArray(ttransforms); currentCubes = new NativeArray <cubeInfo>(cubes.Count, Allocator.Persistent, NativeArrayOptions.UninitializedMemory); }