public override Vector2 CalculateLeadMove(FlockAgent_Follower agent, List <Transform> context, FlockWithLeader flock, Leader_Boidsynth07 leader) { if (leader != null) { leaderPos = leader.transform.position; /* First let's face the agent towards the mouse */ /* subtract the position of agent from mouse position to get vector pointing from * the agent to the mouse */ Vector2 vectorToLeader = new Vector2( leaderPos.x - agent.transform.position.x, leaderPos.y - agent.transform.position.y ); agent.dist = vectorToLeader.magnitude; //Draw a line to mouse position Debug.DrawLine( agent.transform.position, leaderPos, Color.white, 0.0f, true ); return(vectorToLeader); } else { Debug.Log("Leader not found"); return(Vector2.zero); } }
public override Vector2 CalculateLeadMove(FlockAgent_Follower agent, List <Transform> context, FlockWithLeader flock, Leader_Boidsynth07 leader) { /* If no neighbors maintain current alignment (heading) * In this case, if no neighbors are found, the agent will move forward * on its current heading. */ if (leader != null) { return(Vector2.zero); } // get the average alignment of neighbors and face that direction Vector2 alignmentMove = Vector2.zero; foreach (Transform item in context) { alignmentMove += (Vector2)item.transform.up; } alignmentMove /= context.Count; // alignmentMove = Vector2.SmoothDamp( // agent.transform.up, // alignmentMove, // ref agent.currentVelocity, // agent.agentSmoothTime // ); return(alignmentMove); //return Seek(agent, agent.transform.position,alignmentMove); }
public override Vector2 CalculateLeadMove(FlockAgent_Follower agent, List <Transform> context, FlockWithLeader flock, Leader_Boidsynth07 leader) { /* If we don't find any neighbors then make no adjustments. * That means to throw a vector with no magnitude. */ if (context.Count == 0) { return(Vector2.zero); } // Add all points together and average. (trying to find a point in the middle of the group) Vector2 avoidanceMove = Vector2.zero; // How many agents are in our avoidance radius int nAvoid = 0; //go through each transform foreach (Transform item in context) { // calculate if the transform is within our avoidance radius if (Vector2.SqrMagnitude(item.position - agent.transform.position) < flock.SquareAvoidanceRadius) { nAvoid++; avoidanceMove += (Vector2)(agent.transform.position - item.position); } } if (nAvoid > 0) { avoidanceMove /= nAvoid; } return(avoidanceMove); }
// Use this for initialization void Start() { squareMaxSpeed = maxSpeed * maxSpeed; squareNeighborRadius = neighborRadius * neighborRadius; squareAvoidanceRadius = squareNeighborRadius * avoidanceRadiusMultiplier * avoidanceRadiusMultiplier; //Imstantiate the leader leader = Instantiate( leaderPrefab, Random.insideUnitCircle * 3, Quaternion.Euler(Vector3.forward * Random.Range(0f, 360f)), transform ); // Populate the scene with flock agents for (int i = 0; i < startingCount; i++) { FlockAgent_Follower newAgent = Instantiate( agentPrefab, Random.insideUnitCircle * 3, Quaternion.Euler(Vector3.forward * Random.Range(0f, 360f)), transform ); newAgent.name = "Agent " + i; newAgent.noteMode = noteMode; newAgent.GetComponent <AudioSource>().volume = 1.0f / startingCount; agents.Add(newAgent); } }
List <Transform> GetNearbyAgents(FlockAgent_Follower agent) { List <Transform> context = new List <Transform>(); // Create an array of colliders by getting all of the colliders in a surrounding radius Collider2D[] contextColliders = Physics2D.OverlapCircleAll(agent.transform.position, neighborRadius); foreach (Collider2D c in contextColliders) { if (c != agent.agentCollider) { context.Add(c.transform); } } return(context); }
public override Vector2 CalculateLeadMove( FlockAgent_Follower agent, List <Transform> context, FlockWithLeader flock, Leader_Boidsynth07 leader) { // Check if the amount of weights and behaviors is the same if (weights.Length != behaviors.Length) { // Throw and error and don't move Debug.LogError("Data mismatch in " + name, this); return(Vector2.zero); } // Set up Move Vector2 move = Vector2.zero; // Iterate through the behaviors for (int i = 0; i < behaviors.Length; i++) { Vector2 partialMove = behaviors[i].CalculateLeadMove(agent, context, flock, leader) * weights[i]; // if movement is not zero if (partialMove != Vector2.zero) { if (partialMove.sqrMagnitude > weights[i] * weights[i]) { // normalize to a magnitude of 1 and multiply it by the weights partialMove.Normalize(); partialMove *= weights[i]; } move += partialMove; } } return(move); }
void Awake() { synth = this.GetComponentInParent <Hv_Boidsynth07_AudioLib>(); agent = this.GetComponentInParent <FlockAgent_Follower>(); angles = this.GetComponentInParent <AngleDetection>(); }
Vector2 Sensors(Vector2 move, FlockAgent_Follower agent) { agent.avoiding = false; float avoidMultiplier = 0.0f; int directionPicker; //Debug.Log(agent.name + " screenviewpos = " + agent.screenViewPos); if (agent.screenViewPos.x < avoidingBoundaryMin || agent.screenViewPos.x > avoidingBoundaryMax) { agent.avoiding = true; directionPicker = Random.Range(1, 2); switch (directionPicker) { case 1: avoidMultiplier -= avoidMultiplierScalar; break; case 2: avoidMultiplier += avoidMultiplierScalar; break; default: break; } } if (agent.screenViewPos.y < avoidingBoundaryMin || agent.screenViewPos.y > avoidingBoundaryMax) { agent.avoiding = true; directionPicker = Random.Range(1, 2); switch (directionPicker) { case 1: avoidMultiplier -= avoidMultiplierScalar; break; case 2: avoidMultiplier += avoidMultiplierScalar; break; default: break; } } if (agent.avoiding) { Debug.Log(agent.name + " avoiding"); move *= avoidMultiplier; agent.agentSmoothTime = agent.avoidingSmoothTime; } else { agent.agentSmoothTime = agent.normalSmoothTime; } move = Vector2.SmoothDamp( agent.transform.up, move, ref agent.currentVelocity, agent.agentSmoothTime ); return(move); }
public abstract Vector2 CalculateLeadMove( FlockAgent_Follower agent, List <Transform> context, FlockWithLeader flock, Leader_Boidsynth07 leader);