void Start() { // The next section is explained in the blogging section of my documentation squareMaxSpeed = maxSpeed * maxSpeed; squareNeighbourRadius = neighbourRadius * neighbourRadius; squareAvoidanceRadius = squareNeighbourRadius * avoidanceRadiusMultiplier * avoidanceRadiusMultiplier; // Instanciate our flock: for (int i = 0; i < startingCount; i++) { FlockAgent newAgent = Instantiate( // This is the object we will be instanciating: agentPrefab, // Set the spawn point for our agents: // We use the AgentDensity constant so that they are always similarly placed - no huge gaps Random.insideUnitCircle * startingCount * AgentDensity, // Now we set the rotation (on the z axis) of each agent // It requires a quaternion, so I am going to create a random Vector3 between 0 and 360 and then convert it to a quaternion Quaternion.Euler(Vector3.forward * Random.Range(0f, 360f)), // Set the parent of the agent - the flock itself's transform transform ); // Give the agents a name so we can keep track of them more easily newAgent.name = "Agent " + i; // When the agent is created it gets added to it's particular flock newAgent.Initialise(this); // Now add each agent to our list of agents agents.Add(newAgent); } }
// Start is called before the first frame update void Start() { squareMaxSpeed = maxSpeed * maxSpeed; squareNeighbourRadius = neighbourRadius * neighbourRadius; squareAvoidanceRadius = squareNeighbourRadius * avoidanceRadiusMultiplier * avoidanceRadiusMultiplier; for (int i = 0; i < startingCount; i++) { FlockAgent newAgent = Instantiate (agentPrefab, Random.insideUnitSphere * startingCount * AgentDensity, Quaternion.Euler(Vector3.forward * Random.Range(0, 360f)), transform); newAgent.name = "Agent " + i; newAgent.Initialise(this); agents.Add(newAgent); } }