/// <summary> /// Get agent position inside the defensive group; /// </summary> /// <param name="agent"></param> /// <param name="targetPosition"></param> /// <param name="isLeft"></param> /// <returns></returns> public Vector3 GetDefenceGroupLocation(SoccerAgent agent, Vector3 targetPosition, bool isLeft) { //Get list of agent according the team; List <SoccerAgent> team = GetAgentTeam(isLeft); //Rank the distance between the player and the target point, the closest to the target point the top of the list; team.Sort((a, b) => { return(Vector3.Distance(a.transform.position, targetPosition).CompareTo(Vector3.Distance(b.transform.position, targetPosition))); }); //Get agent index value after sorting; var index = team.FindIndex(a => a.getNum() == agent.getNum()); // if the index value is 0, the target is the ball. if (index <= 0) { return(targetPosition); } else { //Align the agent to the nearest player who is closer to the ball var nearsMeAgentLocation = team[index - 1].transform.position; if (transform.position.z > nearsMeAgentLocation.z) { return(new Vector3(nearsMeAgentLocation.x, 0, nearsMeAgentLocation.z + 3)); } else { return(new Vector3(nearsMeAgentLocation.x, 0, nearsMeAgentLocation.z - 3)); } } }
/// <summary> /// Get the position in the attacker team;; /// </summary> /// <param name="agent"></param> /// <param name="targetPosition"></param> /// <param name="isLeft"></param> /// <returns></returns> public Vector3 GetAttackGroupLocation(SoccerAgent agent, Vector3 targetPosition, bool isLeft) { //Get a list of players according to the team List <SoccerAgent> team = GetAgentTeam(isLeft); //Rank the distance between the player and the target point, with the closest to the target point will be set at the top of the list; team.Sort((a, b) => { return(Vector3.Distance(a.transform.position, targetPosition).CompareTo(Vector3.Distance(b.transform.position, targetPosition))); }); //Get the index value after sorting; var index = team.FindIndex(a => a.getNum() == agent.getNum()); // Index value of 0 is the closest player to the target point; if (index == 0) { return(targetPosition); } else { // Get the position of the closest player to the target point; var nearstBallAgentLocation = team[0].transform.position; int position = (index + 1) / 2; if ((index % 2) == 0) { //If the index is even, place it on the side of the player closest to the target point; return(new Vector3(nearstBallAgentLocation.x, 0, nearstBallAgentLocation.z - position * 6)); } //If the index is an odd number, place the side of the player nearest to the target point, above the pitch; return(new Vector3(nearstBallAgentLocation.x, 0, nearstBallAgentLocation.z + position * 6)); } }
public override TaskStatus OnUpdate() { nearPlayer = AttackStrategy.Instance.findNear(mAgent, isLeft); if (nearPlayer.getNum() == mAgent.getNum()) { //if we are the closest player to the goal return failure return(TaskStatus.Failure); } else { // if we are not the clost player to the goal. if (Condition.CanKickBall(mAgent.transform.position, ballLoaction)) { mAgent.transform.LookAt(ballLoaction); //shoot the ball to the nearest attacker ball.AddForce(ballLoaction, nearPlayer.transform.position); return(TaskStatus.Success); } return(TaskStatus.Failure); } }