using UnityEngine; using MLAgents; public class AgentSoccer : Agent { public enum Team { Red, Blue } public enum AgentRole { Striker, Goalie } public Team team; public AgentRole agentRole; float kickPower; int playerIndex; public SoccerFieldArea area; [HideInInspector] public Rigidbody agentRb; SoccerAcademy academy; Renderer agentRenderer; RayPerception rayPer; public void ChooseRandomTeam() { team = (Team)Random.Range(0, 2); if (team == Team.Red) { JoinRedTeam(agentRole); } else { JoinBlueTeam(agentRole); } } public void JoinRedTeam(AgentRole role) { agentRole = role; team = Team.Red; agentRenderer.material = academy.redMaterial; tag = "redAgent"; } public void JoinBlueTeam(AgentRole role) { agentRole = role; team = Team.Blue; agentRenderer.material = academy.blueMaterial; tag = "blueAgent"; } public override void InitializeAgent() { base.InitializeAgent(); agentRenderer = GetComponent(); rayPer = GetComponent(); academy = FindObjectOfType(); agentRb = GetComponent(); agentRb.maxAngularVelocity = 500; var playerState = new PlayerState { agentRB = agentRb, startingPos = transform.position, agentScript = this, }; area.playerStates.Add(playerState); playerIndex = area.playerStates.IndexOf(playerState); playerState.playerIndex = playerIndex; } public override void CollectObservations() { float rayDistance = 20f; float[] rayAngles = { 0f, 45f, 90f, 135f, 180f, 110f, 70f }; string[] detectableObjects; if (team == Team.Red) { detectableObjects = new[] { "ball", "redGoal", "blueGoal", "wall", "redAgent", "blueAgent" }; } else { detectableObjects = new[] { "ball", "blueGoal", "redGoal", "wall", "blueAgent", "redAgent" }; } AddVectorObs(rayPer.Perceive(rayDistance, rayAngles, detectableObjects, 0f, 0f)); AddVectorObs(rayPer.Perceive(rayDistance, rayAngles, detectableObjects, 1f, 0f)); } public void MoveAgent(float[] act) { Vector3 dirToGo = Vector3.zero; Vector3 rotateDir = Vector3.zero; int action = Mathf.FloorToInt(act[0]); // Goalies and Strikers have slightly different action spaces. if (agentRole == AgentRole.Goalie) { kickPower = 0f; switch (action) { case 1: dirToGo = transform.forward * 1f; kickPower = 1f; break; case 2: dirToGo = transform.forward * -1f; break; case 4: dirToGo = transform.right * -1f; break; case 3: dirToGo = transform.right * 1f; break; } } else { kickPower = 0f; switch (action) { case 1: dirToGo = transform.forward * 1f; kickPower = 1f; break; case 2: dirToGo = transform.forward * -1f; break; case 3: rotateDir = transform.up * 1f; break; case 4: rotateDir = transform.up * -1f; break; case 5: dirToGo = transform.right * -0.75f; break; case 6: dirToGo = transform.right * 0.75f; break; } } transform.Rotate(rotateDir, Time.deltaTime * 100f); agentRb.AddForce(dirToGo * academy.agentRunSpeed, ForceMode.VelocityChange); } public override void AgentAction(float[] vectorAction, string textAction) { // Existential penalty for strikers. if (agentRole == AgentRole.Striker) { AddReward(-1f / 3000f); } // Existential bonus for goalies. if (agentRole == AgentRole.Goalie) { AddReward(1f / 3000f); } MoveAgent(vectorAction); } /// /// Used to provide a "kick" to the ball. /// void OnCollisionEnter(Collision c) { float force = 2000f * kickPower; if (c.gameObject.CompareTag("ball")) { Vector3 dir = c.contacts[0].point - transform.position; dir = dir.normalized; c.gameObject.GetComponent().AddForce(dir * force); } } public override void AgentReset() { if (academy.randomizePlayersTeamForTraining) { ChooseRandomTeam(); } if (team == Team.Red) { JoinRedTeam(agentRole); transform.rotation = Quaternion.Euler(0f, -90f, 0f); } else { JoinBlueTeam(agentRole); transform.rotation = Quaternion.Euler(0f, 90f, 0f); } transform.position = area.GetRandomSpawnPos(agentRole, team); agentRb.velocity = Vector3.zero; agentRb.angularVelocity = Vector3.zero; area.ResetBall(); } }