public override Vector3 CalculateMove(FlockAgent3D agent, List <Transform> context, Flock3D flock) { //How far is the agent from the center and steer towards th center Vector3 centerOffset = center - agent.transform.position; //tells us where we are, if t i 0 we are at the center if t is 1 we are at the end of the radius. float t = centerOffset.magnitude / (radius * 3.14f); //if we are iside the 0.9 radius let the agent continue their journey if (t < 0.9f) { return(Vector3.zero); } return(centerOffset * t * t / radius); }
public abstract Vector3 CalculateMove(FlockAgent3D agent3D, List <Transform> context3D, Flock3D flock3D);
//Finds the middle point between neighbours and tries to move there public override Vector3 CalculateMove(FlockAgent3D agent, List <Transform> context, Flock3D flock) { //If no neighbors return no adjustment if (context.Count == 0) { return(Vector3.zero); } //Add all points together and avarage Vector3 cohesionMove = Vector3.zero; List <Transform> fiteredContex = (filter == null) ? context : filter.Filter(agent, context); foreach (Transform item in fiteredContex) { cohesionMove += item.position; } cohesionMove /= context.Count; //create offset from agent position cohesionMove -= (Vector3)agent.transform.position; return(cohesionMove); }
//Finds the middle point between neighbours and tries to move there public override Vector3 CalculateMove(FlockAgent3D agent, List <Transform> context, Flock3D flock) { //If no neighbors return no adjustment if (context.Count == 0) { return(Vector3.zero); } //Add all points together and avarage Vector3 AvoidanceMove = Vector3.zero; //How many inside the avodance radius. int nAvoid = 0; List <Transform> fiteredContex = (filter == null) ? context : filter.Filter(agent, context); foreach (Transform item in fiteredContex) { Vector3 closestPoint = item.gameObject.GetComponent <Collider>().ClosestPoint(agent.transform.position); //squared distance between the item and the agent if (Vector3.SqrMagnitude(closestPoint - agent.transform.position) < flock.SquareAvoidanceRadius) { //gives the offset AvoidanceMove += (agent.transform.position - closestPoint); //adds nAvoid++; } } if (nAvoid > 0) { AvoidanceMove /= nAvoid; } return(AvoidanceMove); }
public void initialize(Flock3D flock) { agentFlock3D = flock; }
//Finds the middle point between neighbours and tries to move there public override Vector3 CalculateMove(FlockAgent3D agent, List <Transform> context, Flock3D flock) { //If no neighbors, maintain current heading if (context.Count == 0) { return(agent.transform.up); } //Add all points together and avarage Vector3 AlignmentMove = Vector3.zero; List <Transform> fiteredContex = (filter == null) ? context : filter.Filter(agent, context); foreach (Transform item in fiteredContex) { AlignmentMove += item.transform.up; } //returns a normalized value AlignmentMove /= context.Count; return(AlignmentMove); }
public override Vector3 CalculateMove(FlockAgent3D agent, List <Transform> context, Flock3D flock) { //Handle data missmatch if (weights.Length != behaviours.Length) { Debug.Log("Error: Data missmactch in" + name, this); return(Vector3.zero); } // setup Move Vector3 move = Vector3.zero; //iterate through behaviours for (int i = 0; i < behaviours.Length; i++) { //middle man passing through the different beaviours Vector3 partialMove = behaviours[i].CalculateMove(agent, context, flock) * weights[i]; //is some movement being returned? if (partialMove != Vector3.zero) { //does this movent exceed the wieight? if (partialMove.sqrMagnitude > weights[i] * weights[i]) { //if it does, normalize it back to magnitude of 1 and multiply it by the weigth so it set perfectly at the maximum of the weight partialMove.Normalize(); partialMove *= weights[i]; } move += partialMove; } } return(move); }