private void Update() { if (distroyedAll == false) { // update boid data for (int i = 0; i < _boidCount; i++) { if (Boids.Count <= 0) { continue; } _boids[i] = new BoidData { Position = Boids[i].transform.position, Forward = Boids[i].transform.forward, NoiseOffset = Boids[i].NoiseOffset }; } } // create boid job UpdateBoidJob boidJob = new UpdateBoidJob { Boids = _boids, BoidTuning = _boidTuning, TargetPosition = _boidTarget.position, Time = Time.timeSinceLevelLoad, DeltaTime = Time.deltaTime }; boidJob.Schedule(_boidTransforms).Complete(); }
void FixedUpdate() { Vector2 moveDir = new Vector2(Input.GetAxis("Horizontal"), Input.GetAxis("Vertical")); bool joinButtonDown = Input.GetKey(KeyCode.Space); if (playerControl) { // If there is no move input and the flock state isn't stationary if (flockState != FlockState.Stationary && moveDir == Vector2.zero) { flockState = FlockState.Stationary; SetAgentsHomePoints(); } // If there is move input and the flock state is not moving else if ((moveDir != Vector2.zero || joinButtonDown) && flockState != FlockState.Moving) { flockState = FlockState.Moving; } } var agentPositions = new NativeArray <Vector2>(flockAgents.Count, Allocator.TempJob); var agentVelocities = new NativeArray <Vector2>(flockAgents.Count, Allocator.TempJob); // Populate position and velocity arrays for (int i = 0; i < flockAgents.Count; i++) { agentVelocities[i] = flockAgents[i].velocity; agentPositions[i] = flockAgents[i].transform.position; } // Cant pass the camera (reference type) into the job, so instead pass // the information the job needs from the camera float height = boundCamera.orthographicSize * 2.0f; var jobHandles = new List <JobHandle>(); for (int i = 0; i < flockAgents.Count; i++) { UpdateBoidJob updateBoidJob = new UpdateBoidJob { index = i, startPosition = transform.position, attractToStart = attractToHome, playerControl = playerControl, homePoint = flockAgents[i].HomePoint, flockState = flockState, cameraHeight = height, cameraWidth = height * Screen.width / Screen.height, cameraPosition = boundCamera.transform.position, moveDirection = moveDir, sightRadius = joinButtonDown ? float.MaxValue : sightRadius, avoidanceRadius = avoidanceRadius, velocityLimit = velocityLimit, percentGravity = percentageGravity, percentMatchVelocity = percentageMatchVelocity, percentAvoidance = percentageAvoidance, percentBoundForce = percentageBoundForce, percentMouseForce = Random.Range(0, 11) % 10 == 0 ? percentageMouseForce : 0, velocities = agentVelocities, positions = agentPositions }; jobHandles.Add(updateBoidJob.Schedule()); } for (int i = 0; i < flockAgents.Count; i++) { jobHandles[i].Complete(); flockAgents[i].velocity = agentVelocities[i]; flockAgents[i].transform.position += new Vector3(flockAgents[i].velocity.x, flockAgents[i].velocity.y, 0); } UpdateAveragePosition(); agentPositions.Dispose(); agentVelocities.Dispose(); }